• 685 阅读
  • 1 回复

C#动态编译类

视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)

代码 复制 - 运行

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;  
  
  
        }  
  
        /// <summary>  
        /// 返回创建类的方法  
        /// </summary>  
        /// <param name="methodName"></param>  
        /// <returns></returns>  
        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);  



http://blog.csdn.net/ff1990723/article/details/51899786
小鱼的淘宝店铺-多多支持哇
视频在线上传+队列转换FLV+水印+捉图+修复+获时+转3GP(API语言不限,开视频站必备!)
如何用C#动态编译、执行代码
http://blog.csdn.net/21aspnet/article/details/3281147
小鱼的淘宝店铺-多多支持哇