.NET 2.0 用のアプリケーションを入手しました。クラス情報の収集にリフレクションを使用したい。しかし、最初にフォルダー内の .cs ファイルをコンパイルする必要があります。アプリケーションからどのように行うことができますか? 私のアプリから自動的にそれを行うことは非常に重要です。たとえば、.cs ファイルを含むフォルダーへのパスを渡すことができるメソッドが必要であり、このメソッドはすべての .cs ファイルをコンパイルします。
2 に答える
1
次のようなことができます。
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.CodeDom;
public static Assembly CreateFromCSFiles(string pathName)
{
CSharpCodeProvider csCompiler = new CSharpCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateInMemory = true;
// here you must add all the references you need.
// I don't know whether you know all of them, but you have to get them
// someway, otherwise it can't work
compilerParams.ReferencedAssemblies.Add("system.dll");
compilerParams.ReferencedAssemblies.Add("system.Data.dll");
compilerParams.ReferencedAssemblies.Add("system.Windows.Forms.dll");
compilerParams.ReferencedAssemblies.Add("system.Drawing.dll");
compilerParams.ReferencedAssemblies.Add("system.Xml.dll");
DirectoryInfo csDir = new DirectoryInfo(pathName);
FileInfo[] files = csDir.GetFiles();
string[] csPaths = new string[files.Length];
foreach (int i = 0; i < csPaths.Length; i++)
csPaths[i] = files[i].FullName;
CompilerResults result = csCompiler.CompileAssemblyFromFile(compilerParams, csPaths);
if (result.Errors.HasErrors)
return null;
return result.CompiledAssembly;
}
于 2012-08-27T13:59:52.627 に答える
0
プログラムとコマンドラインの両方で cs ファイルをコンパイルできます。プログラムでそれを行うには、使用する必要がありCSharpCodeProvider
ます
于 2012-08-27T04:23:39.027 に答える