Can someone explain me the functionality of the Assembly Class to me?

五迷三道 提交于 2020-01-24 00:19:53

问题


I want to Load and Create Assemblies during runtime and someone told me to use the Namespace System.Reflection.Assembly and System.Reflection.Emit.

Only reference I found was on the msdn, but it´s not as good to work with it when you don´t know where and how to start. I already googled but I didn´t find any useful tutorials/samples/references.

Can someone explain the functionality to me or give me some samples/tutorials?


回答1:


http://msdn.microsoft.com/en-us/library/saf5ce06.aspx

 public static void CompileScript(string source)
        {
            CompilerParameters parms = new CompilerParameters();
            parms.GenerateExecutable = true;
            parms.GenerateInMemory = true;
            parms.IncludeDebugInformation = false;
            parms.ReferencedAssemblies.Add("System.dll");
            // Add whatever references you might need here 
            CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
            CompilerResults results = compiler.CompileAssemblyFromSource(parms, source);
            file.move(results.CompiledAssembly.Location,"c:\myassembly.dll");
        }



回答2:


One possible way to create assembly from a source file(s) is simply run CSC (C# command line compiler) passing in source files and references as arguments. Manual IL generation is likely way too advanced, especially if you want to build assemblies from code provided by someone else.

To load - use Assembly.Load.



来源:https://stackoverflow.com/questions/12680576/can-someone-explain-me-the-functionality-of-the-assembly-class-to-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!