C#在运行时动态创建类型的实现方法
程序员文章站
2023-12-16 22:59:10
本文实例讲述了c#在运行时动态创建类型的实现方法。是c#项目开发中很实用的技巧。分享给大家供大家参考。具体分析如下:
具体来说,c# 在运行时动态的创建类型是通过动态生成...
本文实例讲述了c#在运行时动态创建类型的实现方法。是c#项目开发中很实用的技巧。分享给大家供大家参考。具体分析如下:
具体来说,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(); }
希望本文所述对大家的c#程序设计有所帮助