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 = ; // 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 > )
{
for( int i = ; 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# 在运行时动态创建类型的更多相关文章

  1. logback运行时动态创建日志文件

    package com.example.demo.config; import ch.qos.logback.classic.Level; import ch.qos.logback.classic. ...

  2. [转] Java运行时动态生成class的方法

    [From] http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 廖雪峰 / 编 ...

  3. Java 运行时动态生成class

    转载 http://www.liaoxuefeng.com/article/0014617596492474eea2227bf04477e83e6d094683e0536000 Java是一门静态语言 ...

  4. LINQ to SQL 运行时动态构建查询条件

    在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...

  5. 使用javassist运行时动态重新加载java类及其他替换选择

    在不少的情况下,我们需要对生产中的系统进行问题排查,但是又不能重启应用,java应用不同于数据库的存储过程,至少到目前为止,还不能原生的支持随时进行编译替换,从这种角度来说,数据库比java的动态性要 ...

  6. .NET6运行时动态更新限流阈值

    昨天博客园撑不住流量又崩溃了,很巧正在编写这篇文章,于是产生一个假想:如果博客园用上我这个限流组件会怎么样呢? 用户会收到几个429错误,并且多刷新几次就看到了内容,不会出现完全不可用. 还可以降低查 ...

  7. C++高效安全的运行时动态类型转换

    关键字:static_cast,dynamic_cast,fast_dynamic_cast,VS 2015. OS:Window 10. C++类之间类型转换有:static_cast.dynami ...

  8. 解决 Retrofit 多 BaseUrl 及运行时动态改变 BaseUrl ?

    原文地址: juejin.im/post/597856- 解决Retrofit多BaseUrl及运行时动态改变BaseUrl(一) 解决Retrofit多BaseUrl及运行时动态改变BaseUrl( ...

  9. 运行时动态库:not found 及介绍-linux的-Wl,-rpath命令

    ---此文章同步自我的CSDN博客--- 一.运行时动态库:not found   今天在使用linux编写c/c++程序时,需要用到第三方的动态库文件.刚开始编译完后,运行提示找不到动态库文件.我就 ...

随机推荐

  1. QT Creator 代码自动补全快捷键设置

    在QT Creater界面点Tools再进Options...(找到environment->Keyboard)在里面找到FakeVim目录下的,发现默认快捷键就是CTRL+SPACE,把它删除 ...

  2. 小白日记22:kali渗透测试之提权(二)--抓包嗅探

    抓包嗅探 通过抓包嗅探目标机器的流量,发现账号密码. Windows系统 1.Wirehshark 2.Omnipeek 3.commview 4.Sniffpass 只会抓取识别传输密码的明文协议, ...

  3. 使用OPC的方式去连接PLC进行AB SLC-5_04数据的采集

    1.  必备软件 Rslinx classic 2.57 .net framework 2.0 VS2013 OS: win7 enterprise x64 2.  软件安装 2.1.安装RSlinx ...

  4. 玩转Android之手摸手教你DIY一个抢红包神器!

    AccessibilityService是Google专门为残障人士设计的一个服务,可以让他们更方便的来操作手机.AccessibilityService一个主要功能是通过监听窗口的变化来判断用户当前 ...

  5. cocos2d-x lua 使用http(下载图片, POST JSON)

    cocos2d-x lua 使用http(下载图片, POST JSON) version: cocos2d-x 3.6 1.使用http post json与服务器交互 require(" ...

  6. hadoop学习记录(三)HBase基本概念

    这一次开始学习HBase数据库. 我用的是VMWare + ubuntu16.04 +Hbase1.1.5 +hadoop2.6.0的组合. 经过亲自安装验证,版本间没有发生冲突,可以进行学习和开发. ...

  7. 查看Oracle执行计划的几种方法

    查看Oracle执行计划的几种方法 一.通过PL/SQL Dev工具 1.直接File->New->Explain Plan Window,在窗口中执行sql可以查看计划结果.其中,Cos ...

  8. 安卓Design包之Toolbar控件的使用

    转自:ToolBar的使用 ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话, ...

  9. mongoDB 用java连接

    (1)下载连接java 的驱动包mongo-2.10.1.jar 下载网址 https://github.com/mongodb/mongo-java-driver/downloads (2)导入下载 ...

  10. 改变linux shell前景色和背景色

    作者:马 岩(Furzoom) (http://www.cnblogs.com/furzoom/)版权声明:本文的版权归作者与博客园共同所有.转载时请在明显地方注明本文的详细链接,未经作者同意请不要删 ...