Using the C# code provider and the ICodeCompiler.CompileAssemblyFromSource
method, I am attempting to compile a code file in order to produce an executable assembly.
使用C#代码提供程序和ICodeCompiler.CompileAssemblyFromSource方法,我试图编译代码文件以生成可执行程序集。
The code that I would like to compile makes use of features such as optional parameters and extension methods that are only available when using the language C# 4.
我想编译的代码使用了可选参数和扩展方法等功能,这些功能仅在使用C#4语言时才可用。
Having said that, the code that I would like to compile only requires (and needs) to target version 2.0 of the .NET Framework.
话虽如此,我想编译的代码只需要(并且需要)以.NET Framework 2.0版为目标。
Using the proceeding code it is possible to avoid any compile-time errors pertaining to syntax however, the resulting assembly will target version 4.0 of the framework which is undesirable.
使用前面的代码可以避免任何与语法有关的编译时错误,但是,生成的程序集将针对框架的4.0版本,这是不合需要的。
var compiler = new CSharpCodeProvider(
new Dictionary<string, string> { { "CompilerVersion", "v4.0" } } );
How can I make is so that the code provider targets language version 4.0 but produces an assembly that only requires version 2.0 of the framework?
我怎样才能使代码提供程序针对语言版本4.0但生成一个只需要框架版本2.0的程序集?
1 个解决方案
#1
10
You need to instruct the C# compiler (that CSharpCodeProvider uses indirectly) that you want to link to another mscorlib.dll, using the /nostdlib option. Here is a sample that should do it:
您需要使用/ nostdlib选项指示要链接到另一个mscorlib.dll的C#编译器(CSharpCodeProvider间接使用)。以下是应该执行此操作的示例:
static void Main(string[] args)
{
// defines references
List<string> references = new List<string>();
// get a reference to the mscorlib you want
var mscorlib_2_x86 = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
@"Microsoft.NET\Framework\v2.0.50727\mscorlib.dll");
references.Add(mscorlib_2_x86);
// ... add other references (System.dll, etc.)
var provider = new CSharpCodeProvider(
new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters(references.ToArray(), "program.exe");
parameters.GenerateExecutable = true;
// instruct the compiler not to use the default mscorlib
parameters.CompilerOptions = "/nostdlib";
var results = provider.CompileAssemblyFromSource(parameters,
@"using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello world from CLR version: "" + Environment.Version);
}
}");
}
If you run this, it should compile a program.exe file. If you run that file, it should display something like this:
如果你运行它,它应该编译program.exe文件。如果你运行该文件,它应该显示如下:
Hello world from CLR version: 2.0.50727.7905
#1
10
You need to instruct the C# compiler (that CSharpCodeProvider uses indirectly) that you want to link to another mscorlib.dll, using the /nostdlib option. Here is a sample that should do it:
您需要使用/ nostdlib选项指示要链接到另一个mscorlib.dll的C#编译器(CSharpCodeProvider间接使用)。以下是应该执行此操作的示例:
static void Main(string[] args)
{
// defines references
List<string> references = new List<string>();
// get a reference to the mscorlib you want
var mscorlib_2_x86 = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
@"Microsoft.NET\Framework\v2.0.50727\mscorlib.dll");
references.Add(mscorlib_2_x86);
// ... add other references (System.dll, etc.)
var provider = new CSharpCodeProvider(
new Dictionary<string, string> { { "CompilerVersion", "v4.0" } });
var parameters = new CompilerParameters(references.ToArray(), "program.exe");
parameters.GenerateExecutable = true;
// instruct the compiler not to use the default mscorlib
parameters.CompilerOptions = "/nostdlib";
var results = provider.CompileAssemblyFromSource(parameters,
@"using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(""Hello world from CLR version: "" + Environment.Version);
}
}");
}
If you run this, it should compile a program.exe file. If you run that file, it should display something like this:
如果你运行它,它应该编译program.exe文件。如果你运行该文件,它应该显示如下:
Hello world from CLR version: 2.0.50727.7905