C# 在运行时动态创建类型
2018-06-27 09:49:00来源:未知 阅读 ()
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(); }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- C++ 析构函数 2020-06-03
- 测量C++程序运行时间 2020-04-17
- [题记-动态规划] 编辑距离 - leetcode 2020-04-06
- STL之vector 2020-04-06
- 递归函数使用动态数组遇到的问题 2020-03-26
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash