动态生成一个继承接口的类

接口:

namespace LibertyPortal.Main.IDAL
{
    public interface Interface1
    {
        void www();
    }
}   

调用

        Assembly assembly = NewAssembly();
        Interface1 www = assembly.CreateInstance("xml.tables.Test1") as Interface1;

方法

public static Assembly NewAssembly()
    {
        //foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
        //{
        //    string wzcxzww = asm.Location;
        //}
        //创建编译器实例。
        CSharpCodeProvider provider = new CSharpCodeProvider();
        //设置编译参数。
        CompilerParameters paras = new CompilerParameters();
        paras.GenerateExecutable = false;
        paras.GenerateInMemory = true;


        string wwasw= System.Web.HttpContext.Current.Server.MapPath("Bin");
        paras.ReferencedAssemblies.Add(wwasw+"http://www.cnblogs.com/cuihongyu3503319/admin/file://libertyportal.main.idal.dll%22);//要真实地址
        paras.ReferencedAssemblies.Add("System.dll");//全局的不用


        CodeCompileUnit m_CodeCompileUnit;
        CodeNamespace m_CodeNameSpace;//命名空间
        CodeTypeDeclaration m_Class;//类
        m_CodeCompileUnit = new CodeCompileUnit();
        m_CodeNameSpace = new CodeNamespace("xml.tables");
        m_CodeCompileUnit.Namespaces.Add(m_CodeNameSpace);
        m_Class = new CodeTypeDeclaration("Test1");//类名为Test1
        m_Class.IsClass = true;//是类


        //产生www方法
        CodeTypeReference voidReference = new CodeTypeReference("System.void");//方法无返回值
        CodeMemberMethod Test = new CodeMemberMethod();//方法
        Test.ReturnType = voidReference;
        Test.Name = "www";
        Test.Attributes = MemberAttributes.Public | MemberAttributes.Final;
        //CodeVariableReferenceExpression m_AddFlagExpression = new CodeVariableReferenceExpression("m_AddFlag");

        //CodeAssignStatement assignAddTrue = new CodeAssignStatement(m_AddFlagExpression, new CodePrimitiveExpression(true));
        //Test.Statements.Add(assignAddTrue);
        m_Class.Members.Add(Test);


        //m_Class.BaseTypes.Add(new CodeTypeReference(typeof(Object)));
        m_Class.BaseTypes.Add(new CodeTypeReference(typeof(Interface1)));//类父亲
        m_CodeNameSpace.Types.Add(m_Class);


        #region 输出生成源代码
        ICodeGenerator g = provider.CreateGenerator();
        g.GenerateCodeFromType(m_Class,System.Web.HttpContext.Current.Response.Output , null);  

        #endregion

        CompilerResults result = provider.CompileAssemblyFromDom(paras, m_CodeCompileUnit);//编译


        if (result.Errors.Count > 0)
        {
            // 显示编译错误信息。  
            string www = "";
            foreach (CompilerError ce in result.Errors)
            {
                www += ce.ToString();
                //Debug.WriteLine();
            }
            System.Web.HttpContext.Current.Response.Write(www);
            return null;
        }
        else
        {
            Debug.WriteLine(result.PathToAssembly);
        }

        //编译代码。
        //CompilerResults result = provider.CompileAssemblyFromSource(paras, classSource.ToString());

        //获取编译后的程序集。
        Assembly assembly = result.CompiledAssembly;


        return assembly;
    }

原文地址:https://www.cnblogs.com/cuihongyu3503319/p/1625888.html