我正在使用 c# 创建一个自定义代码生成器......这个生成器现在只从数据库的表中创建业务实体。但现在我想要的是创建一个包含这些类的项目,并在创建之后......从项目中生成 dll。
有没有办法完成这项工作?
我正在使用 c# 创建一个自定义代码生成器......这个生成器现在只从数据库的表中创建业务实体。但现在我想要的是创建一个包含这些类的项目,并在创建之后......从项目中生成 dll。
有没有办法完成这项工作?
您可以使用CSharpCodeProvider将代码编译为 DLL,如下所示(在互联网某处找到):
public static bool CompileCSharpCode(String sourceFile, String outFile)
{
// Obtain an ICodeCompiler from a CodeDomProvider class.
CSharpCodeProvider provider = new CSharpCodeProvider();
ICodeCompiler compiler = provider.CreateCompiler(); // Build the parameters for source compilation.
CompilerParameters cp = new CompilerParameters(); // Add an assembly reference.
cp.ReferencedAssemblies.Add("System.dll"); // Generate an class library instead of // a executable.
cp.GenerateExecutable = false; // Set the assembly file name to generate.
cp.OutputAssembly = outFile; // Save the assembly as a physical file.
cp.GenerateInMemory = false; // Invoke compilation.
CompilerResults cr = compiler.CompileAssemblyFromFile(cp, sourceFile);
if (cr.Errors.Count > 0)
{ // Display compilation errors.
Console.WriteLine("Errors building {0} into {1}", sourceFile, cr.PathToAssembly);
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Console.WriteLine();
}
}
else
{
Console.WriteLine("Source {0} built into {1} successfully.", sourceFile, cr.PathToAssembly);
} // Return the results of compilation.
if (cr.Errors.Count > 0) { return false; } else { return true; }
}