Csharp/C#教程:C#在运行时动态创建类型的实现方法分享

本文实例讲述了C#在运行时动态创建类型的实现方法。是C#项目开发中很实用的技巧。分享给大家供大家参考。具体分析如下:

具体来说,C#在运行时动态的创建类型是通过动态生成C#源代码,然后通过编译器编译成程序集的方式实现动态创建类型的。

主要功能代码如下:

publicstaticAssemblyNewAssembly() { //创建编译器实例。 provider=newCSharpCodeProvider(); //设置编译参数。 cp=newCompilerParameters(); cp.GenerateExecutable=false; cp.GenerateInMemory=true; //Generateanexecutableinsteadof //aclasslibrary. //cp.GenerateExecutable=true; //Settheassemblyfilenametogenerate. cp.OutputAssembly="c:\1.dll"; //Generatedebuginformation. cp.IncludeDebugInformation=true; //Savetheassemblyasaphysicalfile. cp.GenerateInMemory=false; //Setthelevelatwhichthecompiler //shouldstartdisplayingwarnings. cp.WarningLevel=3; //Setwhethertotreatallwarningsaserrors. cp.TreatWarningsAsErrors=false; //Setcompilerargumenttooptimizeoutput. 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"); //创建动态代码。 StringBuilderclassSource=newStringBuilder(); classSource.Append("usingSystem;usingSystem.Windows.Forms;npublicclassDynamicClass:UserControln"); classSource.Append("{n"); classSource.Append("publicDynamicClass()n{nInitializeComponent();nConsole.WriteLine("hello");}n"); classSource.Append("privateSystem.ComponentModel.IContainercomponents=null;nprotectedoverridevoidDispose(booldisposing)n{n"); classSource.Append("if(disposing&&(components!=null)){components.Dispose();}base.Dispose(disposing);n}n"); classSource.Append("privatevoidInitializeComponent(){nthis.SuspendLayout();this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);"); classSource.Append("this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;this.Name="DynamicClass";this.Size=newSystem.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()); //编译代码。 CompilerResultsresult=provider.CompileAssemblyFromSource(cp,classSource.ToString()); if(result.Errors.Count>0) { for(inti=0;i<result.Errors.Count;i++) Console.WriteLine(result.Errors[i]); Console.WriteLine("error"); returnnull; } //获取编译后的程序集。 Assemblyassembly=result.CompiledAssembly; returnassembly; } privatestaticstringpropertyString(stringpropertyName) { StringBuildersbProperty=newStringBuilder(); sbProperty.Append("privateint_"+propertyName+"=0;n"); sbProperty.Append("publicint"+""+propertyName+"n"); sbProperty.Append("{n"); sbProperty.Append("get{return_"+propertyName+";}n"); sbProperty.Append("set{_"+propertyName+"=value;}n"); sbProperty.Append("}"); returnsbProperty.ToString(); }

希望本文所述对大家的C#程序设计有所帮助

您可能感兴趣的文章:C#Dynamic关键字之:调用属性、方法、字段的实现方法c#动态编译执行对象方法示例运用映射机制创建对象C#dynamic关键字的使用方法C#动态对象(dynamic)详解(实现方法和属性的动态)

标签: 动态 方法 运行

Linux中使用C语言实现基于UDP协议的Socket通信示例

使用C语言编写基于TCP协议的Socket通讯程序实例分享

上述就是C#学习教程:C#在运行时动态创建类型的实现方法分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/905741.html

(0)
上一篇 2021年10月23日
下一篇 2021年10月23日

精彩推荐