国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

C#在運(yùn)行時(shí)動(dòng)態(tài)創(chuàng)建類型

2019-11-14 16:27:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

C# 在運(yùn)行時(shí)動(dòng)態(tài)的創(chuàng)建類型,這里是通過(guò)動(dòng)態(tài)生成C#源代碼,然后通過(guò)編譯器編譯成程序集的方式實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建類型

public static Assembly NewAssembly()        {            //創(chuàng)建編譯器實(shí)例。               PRovider = new CSharpCodeProvider();            //設(shè)置編譯參數(shù)。               cp = new CompilerParameters();            cp.GenerateExecutable = false;            cp.GenerateInMemory = true;            // Generate an executable instead of             // a class library.            //cp.GenerateExecutable = true;            // Set the assembly file name to generate.            cp.OutputAssembly = "c://1.dll";            // Generate debug information.            cp.IncludeDebugInformation = true;            // Save the assembly as a physical file.            cp.GenerateInMemory = false;            // Set the level at which the compiler             // should start displaying warnings.            cp.WarningLevel = 3;            // Set whether to treat all warnings as errors.            cp.TreatWarningsAsErrors = false;            // Set compiler argument to optimize output.            cp.CompilerOptions = "/optimize";            cp.ReferencedAssemblies.Add("System.dll");            //cp.ReferencedAssemblies.Add("System.Core.dll");            cp.ReferencedAssemblies.Add("System.Data.dll");            //cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");            cp.ReferencedAssemblies.Add("System.Deployment.dll");            cp.ReferencedAssemblies.Add("System.Design.dll");            cp.ReferencedAssemblies.Add("System.Drawing.dll");            cp.ReferencedAssemblies.Add("System.Windows.Forms.dll");            //創(chuàng)建動(dòng)態(tài)代碼。                           StringBuilder classSource = new StringBuilder();            classSource.Append("using System;using System.Windows.Forms;/npublic   class   DynamicClass: UserControl /n");            classSource.Append("{/n");            classSource.Append("public DynamicClass()/n{/nInitializeComponent();/nConsole.WriteLine(/"hello/");}/n");            classSource.Append( "private System.ComponentModel.IContainer components = null;/nprotected override void Dispose(bool disposing)/n{/n");            classSource.Append( "if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);/n}/n");            classSource.Append( "private void InitializeComponent(){/nthis.SuspendLayout();this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);");            classSource.Append( "this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Name = /"DynamicClass/";this.Size = new System.Drawing.Size(112, 74);this.ResumeLayout(false);/n}");            //創(chuàng)建屬性。               /*************************在這里改成需要的屬性******************************/            classSource.Append(propertyString("aaa"));            classSource.Append(propertyString("bbb"));            classSource.Append(propertyString("ccc"));            classSource.Append("}");            System.Diagnostics.Debug.WriteLine(classSource.ToString());                      //編譯代碼。               CompilerResults result = provider.CompileAssemblyFromSource(cp, classSource.ToString());            if (result.Errors.Count > 0)            {                for( int i = 0; i < result.Errors.Count; i ++)                    Console.WriteLine(result.Errors[ i]);                Console.WriteLine("error");                return null;            }                        //獲取編譯后的程序集。               Assembly assembly = result.CompiledAssembly;            return assembly;        }        private static string propertyString(string propertyName)        {            StringBuilder sbProperty = new StringBuilder();            sbProperty.Append(" private   int   _" + propertyName + "   =   0;/n");            sbProperty.Append(" public   int   " + "" + propertyName + "/n");            sbProperty.Append(" {/n");            sbProperty.Append(" get{   return   _" + propertyName + ";}   /n");            sbProperty.Append(" set{   _" + propertyName + "   =   value;   }/n");            sbProperty.Append(" }");            return sbProperty.ToString();        }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 土默特右旗| 通许县| 芦山县| 五原县| 四会市| 都昌县| 河东区| 阿鲁科尔沁旗| 会同县| 慈利县| 饶阳县| 定兴县| 武邑县| 库车县| 丹巴县| 宁陵县| 安远县| 天全县| 冀州市| 郸城县| 新河县| 寿宁县| 高州市| 若羌县| 东乡| 岳阳县| 桦南县| 莱芜市| 年辖:市辖区| 仁寿县| 大洼县| 藁城市| 温泉县| 全南县| 浪卡子县| 大姚县| 武汉市| 托里县| 舒兰市| 长岛县| 永安市|