C# code to create the DLL

public void Porcess()
    {
        //需要编译的脚本所在文件夹
        string _scriptDir = Common.GetPath(CommonPath.TableProtoScriptPath);
        string[] files = Directory.GetFiles(_scriptDir ,"*.cs");
        string dllPath = Common.GetPath(CommonPath.TableProtoDllPath) + "\\TableProto.dll";
        CompilerDll(files, dllPath);
    }
    /// <summary>
    /// 动态编译并执行代码
    /// </summary>
    /// <param name="code">代码</param>
    /// <returns>返回输出内容</returns>
    public CompilerResults CompilerDll(string[] _class, string newPath)
    {
        CodeDomProvider complier = CodeDomProvider.CreateProvider("CSharp");
        //设置编译参数
        CompilerParameters paras = new CompilerParameters();
        //引入第三方dll
        paras.ReferencedAssemblies.Add("System.dll");
        paras.ReferencedAssemblies.Add("System.RunTime.dll");
        paras.ReferencedAssemblies.Add("GoogleProtobuf.dll");
        //是否内存中生成输出
        paras.GenerateInMemory = false;
        //是否生成可执行文件
        paras.GenerateExecutable = false;
        //输出的Dll目录
        paras.OutputAssembly = newPath;
        //编译代码
        CompilerResults result = complier.CompileAssemblyFromFile(paras, _class);
        foreach (var item in result.Errors)
        {
            // 错误信息
            Console.WriteLine(item.ToString());
            //自定义log显示方法 -- 使用时自己实现
            Debug.Log(item.ToString());
        } 
        return result;
    }

The local code file is as follows:

Generated DLL

 

Decompiled DLL result:

 

Note: .net 5 and above are no longer supported, please use a version below .net 5

Guess you like

Origin blog.csdn.net/LM514104/article/details/128446042