麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

C#在運行時動態創建類型

2019-11-14 16:27:23
字體:
來源:轉載
供稿:網友

C# 在運行時動態的創建類型,這里是通過動態生成C#源代碼,然后通過編譯器編譯成程序集的方式實現動態創建類型

public static Assembly NewAssembly()        {            //創建編譯器實例。               PRovider = new CSharpCodeProvider();            //設置編譯參數。               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");            //創建動態代碼。                           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}");            //創建屬性。               /*************************在這里改成需要的屬性******************************/            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();        }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 性爱视频在线免费 | 亚洲成a人在线 | 日本不卡视频在线观看 | 久久成人激情视频 | 九九看片 | 在线观看网址av | 污在线观看网站 | 成人一区二区三区四区 | 日本道中文字幕 | 亚洲精品一区二区三区在线看 | 日本aaaa片毛片免费观看视频 | 欧美 日韩 亚洲 中文 | 国产色视频免费 | 国产精品午夜未成人免费观看 | 欧美国产成人在线 | 欧美成人黄色小视频 | 污污短视频 | 国产91成人 | 日本成人在线免费 | 思思久而久而蕉人 | 成人毛片在线免费观看 | 草草影院地址 | 香蕉久久久久 | 日韩黄色在线播放 | 在线观看精品视频 | 在线观看福利网站 | 精品一区二区中文字幕 | 久久国产精品二区 | 日本黄色不卡视频 | 日本韩国欧美一级片 | 精品在线视频观看 | 在线观看中文字幕国产 | 欧美一级精品 | 日日操夜夜操视频 | 成人午夜免费在线视频 | www.99久久久 | 日本免费一区二区三区四区 | 九九精品在线 | 成人福利视频 | 黄色电影免费网址 | 粉嫩粉嫩一区二区三区在线播放 |