Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以用代码在类型中添加空白吗_C#_Reflection - Fatal编程技术网

C# 我可以用代码在类型中添加空白吗

C# 我可以用代码在类型中添加空白吗,c#,reflection,C#,Reflection,我有一个这样的编译器代码 Assembly BuildAssembly(string code) { List<string> SKParams = new List<string>(); string Caption = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\

我有一个这样的编译器代码

Assembly BuildAssembly(string code)
        {

            List<string> SKParams = new List<string>();
            string Caption = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\";
            SKParams.Add(Caption + "System" + ".dll");
            SKParams.Add(Caption + "System.Windows.Forms" + ".dll");
            SKParams.Add(Caption + "System.Data" + ".dll");
            SKParams.Add(Caption + "System.Core" + ".dll");
            SKParams.Add(Caption + "System.Drawing" + ".dll");
            SKParams.Add(Caption + "System.Drawing" + ".dll");
            SKParams.Add(@"D:\SK\Projelerim\ZxProject\MySDK\MySDK\bin\Debug\MySDK.dll");
            SKParams.Add(Caption + "System.XML" + ".dll");
            Microsoft.CSharp.CSharpCodeProvider provider = new CSharpCodeProvider();
            ICodeCompiler compiler = provider.CreateCompiler();
            CompilerParameters compilerparams = new CompilerParameters(SKParams.ToArray());

            compilerparams.GenerateExecutable = false;
            compilerparams.GenerateInMemory = true;

            CompilerResults results = compiler.CompileAssemblyFromSource(compilerparams, code);
            if (results.Errors.HasErrors)
            {
                StringBuilder errors = new StringBuilder("Compiler Errors :\r\n");
                foreach (CompilerError error in results.Errors)
                {
                    errors.AppendFormat("Line {0},{1}\t: {2}\n",
                           error.Line, error.Column, error.ErrorText);
                    MessageBox.Show(error.ErrorText);
                }
                throw new Exception(errors.ToString());
            }
            else
            {
                return results.CompiledAssembly;
            }

        }
Assembly BuildAssembly(字符串代码)
{
List SKParams=新列表();
字符串标题=@“C:\ProgramFiles(x86)\Reference Assembly\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\”;
添加(标题+“系统”+“.dll”);
添加(标题+“System.Windows.Forms”+“.dll”);
添加(标题+“System.Data”+“.dll”);
添加(标题+“System.Core”+“.dll”);
添加(标题+“System.Drawing”+“.dll”);
添加(标题+“System.Drawing”+“.dll”);
添加(@“D:\SK\Projelerim\ZxProject\MySDK\MySDK\bin\Debug\MySDK.dll”);
添加(标题+“System.XML”+“.dll”);
Microsoft.CSharp.CSharpCodeProvider provider=新的CSharpCodeProvider();
ICodeCompiler compiler=provider.CreateCompiler();
CompilerParameters compilerparams=新的CompilerParameters(SKParams.ToArray());
compilerparams.GenerateExecutable=false;
compilerparams.GenerateInMemory=true;
CompilerResults results=compiler.compileAsemblyFromSource(compilerparams,代码);
if(results.Errors.HasErrors)
{
StringBuilder错误=新建StringBuilder(“编译器错误:\r\n”);
foreach(结果中的编译器错误。错误)
{
errors.AppendFormat(“行{0},{1}\t:{2}\n”,
error.Line、error.Column、error.ErrorText);
MessageBox.Show(error.ErrorText);
}
抛出新异常(errors.ToString());
}
其他的
{
返回results.CompiledAssembly;
}
}

我使用此方法从字符串代码中获取程序集。现在我想在此程序集中添加一个void,并更改程序集和源代码(字符串代码)。如何实现此目的?

我知道的将方法添加到现有类型的唯一方法是使用System.Reflection.Emit中的DynamicMethod类。您可以在这里获得一些帮助和示例:,但它对用户不太友好。如果要从字符串开始创建此方法,则必须解析该字符串并在一系列调用中对其进行转换,以使用适当的操作码发出。我认为使用更新/添加的代码可以更轻松地重新创建程序集

如果您必须将新添加的方法应用于类的现有实例,我将创建新程序集,创建新类的新实例,然后通过反射复制属性的旧值。

您尝试执行的操作(至少对我来说)不太清楚。您的意思是创建一个程序集时,您的方法向它传递一个定义类的字符串(代码),然后您想向该类添加一个void方法(它不在原始代码中)?