Sco*_*ott 5 c# namespaces class loading text-files
有没有办法将.txt文件作为一个类加载,我的主程序可以从中调用函数?我基本上试图将mod支持添加到我的简单应用程序中,用户可以从每个文件中选择选项.该文件遵循类似(但不相同)的格式,在主程序中调用一堆空白(函数).
我怎样才能做到这一点?这是否可能(可能不是,回答我自己的问题?),考虑到它不会与程序的其余部分一起编译?
谢谢!
请看这个问题的答案:在运行时编译C#数组并在代码中使用它?
基本上,您可能希望将文本文件拉入CodeDom并进行编译.在那之后,创建一些动态方法作为执行助手可能会有所帮助.
var csc = new CSharpCodeProvider( new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } } );
var cp = new CompilerParameters() {
GenerateExecutable = false,
OutputAssembly = outputAssemblyName,
GenerateInMemory = true
};
cp.ReferencedAssemblies.Add( "mscorlib.dll" );
cp.ReferencedAssemblies.Add( "System.dll" );
StringBuilder sb = new StringBuilder();
// The string can contain any valid c# code
sb.Append( "namespace Foo{" );
sb.Append( "using System;" );
sb.Append( "public static class MyClass{");
sb.Append( "}}" );
// "results" will usually contain very detailed error messages
var results = csc.CompileAssemblyFromSource( cp, sb.ToString() );
Run Code Online (Sandbox Code Playgroud)
另请参阅此主题:在C#中实现脚本语言.使用DLR的IronPython可能适合您的需求.