C#动态编译类

创建编译类:

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace H264Player
{
    public class ImportDLLCompiler
    {
        private string _dllPath;
        private string className;

        private Assembly assembly;

        public ImportDLLCompiler(string className,string dllpath="")
        {
            this.className = className;
            _dllPath = dllpath;
            assembly = Compile();
        }

        private Assembly Compile()
        {
            // 编译器 
            CodeDomProvider cdp = CodeDomProvider.CreateProvider("C#");

            // 编译器的参数 
            CompilerParameters cp = new CompilerParameters();
            cp.ReferencedAssemblies.Add("System.dll");
            cp.GenerateExecutable = false;
            cp.GenerateInMemory = true;

            // 编译结果 
            CompilerResults cr = cdp.CompileAssemblyFromSource(cp, generateClass());

            if (cr.Errors.HasErrors) throw new Exception(" ImportDLLCompiler 编译出错!");
            else
                return  cr.CompiledAssembly;


        }

        /// 
        /// 返回创建类的方法
        /// 
        /// 
        /// 
        public MethodInfo GetMethod(string methodName)
        {
            MethodInfo mi = null;
            if (assembly != null)
            {
                Type type = assembly.GetType("H264Player." + className);
                mi = type.GetMethod(methodName);
            }

                return mi;
        
        }

        // 动态构建的代码 
        string generateClass()
        {
            string folder = _dllPath != "" ? _dllPath + "\\" : _dllPath;

            StringBuilder sbCode = new StringBuilder();
            sbCode.AppendLine("using System.Runtime.InteropServices;");
            sbCode.AppendLine("using System.Reflection;");
            sbCode.AppendLine("namespace H264Player");
            sbCode.AppendLine("{");
            sbCode.AppendLine(" class " + className);
            sbCode.AppendLine("    {");
            sbCode.AppendLine("  [DllImport(@\"" + folder + "endata.dll\", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] ");
            sbCode.AppendLine("  public static extern void Install();");
            sbCode.AppendLine("  [DllImport(@\"" + folder + "endata.dll\", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] ");
            sbCode.AppendLine("  public static extern void SaveData(System.IntPtr p_pszInFrame, int p_nInSize, System.IntPtr p_pobjAVFrame, ref int outsize);");
            sbCode.AppendLine("  [DllImport(@\"" + folder + "endata.dll\", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] ");
            sbCode.AppendLine("  public static extern int Install2(int v);");
            sbCode.AppendLine("  [DllImport(@\"" + folder + "endata.dll\", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] ");
            sbCode.AppendLine("  public static extern int UnInstall();");
            sbCode.AppendLine("      }");
            sbCode.AppendLine(" }");
            return sbCode.ToString();
        } 
    }
}



调用:

            var compiler = new ImportDLLCompiler("Import", "c:\1");

            var method = compiler.GetMethod("Install");
            method.Invoke(null, null);


你可能感兴趣的:(C#)