【C#】动态生成DLL

    public string Create_DLL(string classNamespace)
    {
        //string classNamespace/命名空间
        //string className 类名或表名
        String filenameDll= "";
        filenameDll= classNamespace + "DAL.dll";
        //生成一个可编译的单元,这是最根部的东西
        CodeCompileUnit compunit = new CodeCompileUnit();

        //  创建编译器实例//设置编译器对象
        CSharpCodeProvider csprovider = new CSharpCodeProvider();
        ICodeCompiler complier = csprovider.CreateCompiler();
      //  设置编译参数。 
        CompilerParameters paras = new CompilerParameters();
        paras.GenerateExecutable = false;   //编译成exe还是dll
        paras.GenerateInMemory = false;           //是否写入内存,不写入内存就写入磁盘
        paras.OutputAssembly = "d:\\" + filenameDll;         //输出路径
        paras.IncludeDebugInformation = false; //是否产生pdb调试文件      默认是false

        paras.ReferencedAssemblies.Add("System.dll");//添加引用DLL
        paras.ReferencedAssemblies.Add("System.Data.dll");//添加引用DLL
          
        StringBuilder classSource = new StringBuilder();
        classSource.Append("using System;\r\n");
        classSource.Append("using System.Collections.Generic;\r\n");
        classSource.Append("using System.Text;\r\n");
        classSource.Append("using System.Data;\r\n");//#region 命名空间及类名
        classSource.Append("namespace " + classNamespace + ".DAL \r\n");
        classSource.Append("{ \r\n");
       {
            //定义类开始
            classSource.Append("    public class " + classNamespace + "_DAL\r\n");
            classSource.Append("    {  \r\n");

                //定义字段
            classSource.Append("       public  string Name { get; set; } \r\n");
                //定义字段结束
                //定义方法
      classSource.Append("public string GetName(){return \"关注微信公众号:维基动漫\";} \r\n");
          //定义方法结束 classSource.Append(" } \r\n"); //类定义结束 } classSource.Append("} \r\n"); //命名空间定义结束 // System.Diagnostics.Debug.WriteLine(classSource.ToString()); // 调试用。注释掉 // 编译代码。 CompilerResults result = complier.CompileAssemblyFromSource(paras, classSource.ToString()); // 获取编译后的程序集。 //Assembly assembly = result.CompiledAssembly; return classSource.ToString(); //// 动态调用方法。 //object eval = assembly.CreateInstance(className); //MethodInfo method = eval.GetType().GetMethod(methodName); // object reobj = "生成dll成功";// method.Invoke(eval, null); //GC.Collect(); // return reobj; }

参考原文:https://www.cnblogs.com/greefsong/archive/2012/07/19/2599186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
生成动态链接库(DLL)的过程大致如下: 1. 打开Visual Studio,创建一个新的DLL项目。 2. 在项目中添加所需的代码文件和资源文件。 3. 对代码进行编译和调试,确保没有错误。 4. 在生成选项中选择“生成DLL”。 5. 编译并生成DLL文件。 以下是一个简单的C#代码示例,演示如何动态生成DLL: ```csharp using System; using System.CodeDom.Compiler; using System.Reflection; using Microsoft.CSharp; public class GenerateDLL { public static void Main() { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CompilerParameters parameters = new CompilerParameters(); parameters.GenerateExecutable = false; parameters.OutputAssembly = "MyDLL.dll"; CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, @"public class MyDLLClass { public void MyMethod() { Console.WriteLine(""Hello from MyDLL!""); } }"); if (results.Errors.HasErrors) { Console.WriteLine("Compilation failed."); foreach (CompilerError error in results.Errors) { Console.WriteLine(error.ErrorText); } } else { Assembly assembly = results.CompiledAssembly; Type type = assembly.GetType("MyDLLClass"); object instance = Activator.CreateInstance(type); MethodInfo methodInfo = type.GetMethod("MyMethod"); methodInfo.Invoke(instance, null); } } } ``` 这个示例代码演示了如何使用CSharpCodeProvider类来编译C#代码,并将其生成为一个名为“MyDLL.dll”的动态链接库。然后,程序使用反射来加载并调用DLL中的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值