当前位置: 首页 > 编程语言 > C#

分享_15

时间:2023-04-11 01:51:47 C#

在.net4.5beta中使用CSharpCodeProvider我最近安装了VisualStudio11Beta,我正在尝试更新现有的4.0项目以使用4.5。在程序中,它使用CSharpCodeProvider编译一些动态生成的代码。//////编译并返回对已编译程序集的引用///privateAssemblyCompile(){varreferencedDlls=newList{"mscorlib.dll","System.dll","System.Core.dll",};referencedDlls.AddRange(RequiredReferences);varparameters=newCompilerParameters(assemblyNames:referencedDlls.ToArray(),outputName:GeneratedDllFileName,//如果我们当前正在调试,则只包含调试信息includeDebugInformation:Debugger.IsAttached);parameters.TreatWarningsAsErrors=false;parameters.WarningLevel=0;parameters.GenerateExecutable=false;parameters.GenerateInMemory=false;parameters.CompilerOptions="/optimize+/platform:x64";string[]files=Directory.GetFiles(GenerationDirectory,"*.cs");varcompiler=newCSharpCodeProvider(newDictionary{{"CompilerVersion","v4.5"}});varresults=compiler.CompileAssemblyFromFile(参数,文件);if(results.Errors.HasErrors){stringfirstError=string.Format("编译error:{0}",results.Errors[0].ToString());thrownewApplicationException(firstError);}else{returnresults.CompiledAssembly;}}当我将CompilerVersion从{"CompilerVersion","v4.0"}改为{"CompilerVersion","v4.5"}我现在得到一个异常Cannotfindcompilerexecutablecsc.exe差异是因为代码不会使用任何新的4.5特定功能吗?如果您给我们一个简短而完整的程序来演示这个问题会有所帮助,但我可以用“v4.0”验证它是否有效并编译异步方法,假设您在装有VS11的机器上运行测试版安装。演示:以上是C#学习教程:使用CSharpCodeProvider与.net4.5beta共享的所有内容。如果对大家有用,需要详细了解C#学习教程,希望大家多多关注——usingSystem;使用System.Collections使用System.Reflection;使用System.Collections.Generic;使用系统诊断;使用Microsoft.CSharp;使用System.CodeDom.Compiler;namespaceFoo{}classProgram{staticvoidMain(string[]args){varreferencedDll=newList{"mscorlib.dll","System.dll","System.Core.dll",};varparameters=newCompilerParameters(assemblyNames:referencedDll.ToArray(),outputName:"foo.dll",includeDebugInformation:false){TreatWarningsAsErrors=true,//我们不想被警告我们没有等待!WarningLevel=0,GenerateExecutable=false,GenerateInMemory=true};varsource=new[]{"classTest{staticasyncvoidFoo(){}}"};varoptions=newDictionary{{"CompilerVersion","v4.0"}};varcompiler=newCSharpCodeProvider(选项);varresults=compiler.CompileAssemblyFromSource(参数酯,来源);Console.WriteLine("成功?{0}",!results.Errors.HasErrors);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: