引用控件:

DLL下载地址:http://pan.baidu.com/s/1nv2GUWL

    public class TypeCreator
{
public static Type Creator(string ClassName, int PropertiesCount)
{
IDictionary<string, Type> Properties = new Dictionary<string, Type>();
Type t = typeof(string);
Properties.Add(new KeyValuePair<string, Type>("ID", typeof(int)));
for (int i = ; i < PropertiesCount; i++)
{
Properties.Add(new KeyValuePair<string, Type>("FF" + i, t));
}
return Creator(ClassName, Properties);
}
public static Type Creator(string ClassName, IDictionary<string, Type> Properties)
{
//应用程序域
AppDomain currentDomain = System.Threading.Thread.GetDomain(); //AppDomain.CurrentDomain;
//运行并创建类的新实例
TypeBuilder typeBuilder = null;
//定义和表示动态程序集的模块
ModuleBuilder moduleBuilder = null;
MethodBuilder methodBuilder = null;
//表示类型属性
PropertyBuilder propertyBuilder = null;
//定义表示字段
FieldBuilder fieldBuilder = null;
//定义表示动态程序集
AssemblyBuilder assemblyBuilder = null;
//生成中间语言指令
ILGenerator ilGenerator = null;
//帮助生成自定义属性
CustomAttributeBuilder cab = null;
//指定方法属性的标志
MethodAttributes methodAttrs; //Define a Dynamic Assembly
//指定名称,访问模式
assemblyBuilder = currentDomain.DefineDynamicAssembly(new AssemblyName("Test2"), AssemblyBuilderAccess.Run); //Define a Dynamic Module动态模块名称
moduleBuilder = assemblyBuilder.DefineDynamicModule("ModuleName",true); //Define a runtime class with specified name and attributes.
typeBuilder = moduleBuilder.DefineType(ClassName, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.BeforeFieldInit | TypeAttributes.Serializable); cab = new CustomAttributeBuilder(typeof(Castle.ActiveRecord.ActiveRecordAttribute).GetConstructor(Type.EmptyTypes), new object[]);
typeBuilder.SetCustomAttribute(cab);// cab = new CustomAttributeBuilder(typeof(Castle.ActiveRecord.PropertyAttribute).GetConstructor(Type.EmptyTypes), new object[]); methodAttrs = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
foreach (KeyValuePair<string, Type> kv in Properties)
{
// Add the class variable, such as "m_strIPAddress"
fieldBuilder = typeBuilder.DefineField("field_" + kv.Key, kv.Value, FieldAttributes.Public); propertyBuilder = typeBuilder.DefineProperty(kv.Key, System.Reflection.PropertyAttributes.HasDefault, kv.Value, null);
if (kv.Key != "ID")
{
propertyBuilder.SetCustomAttribute(cab);//
}
else
{
propertyBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(Castle.ActiveRecord.PrimaryKeyAttribute).GetConstructor(Type.EmptyTypes), new object[]));//
} methodBuilder = typeBuilder.DefineMethod("get_" + kv.Key, methodAttrs, kv.Value, Type.EmptyTypes);
ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldfld, fieldBuilder);
ilGenerator.Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(methodBuilder); methodBuilder = typeBuilder.DefineMethod("set_" + kv.Key, methodAttrs, typeof(void), new Type[] { kv.Value });
ilGenerator = methodBuilder.GetILGenerator();
ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);
ilGenerator.Emit(OpCodes.Stfld, fieldBuilder);
ilGenerator.Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(methodBuilder);
}
//Create Class
return typeBuilder.CreateType();
return assemblyBuilder.GetType(ClassName);
return moduleBuilder.GetType(ClassName); }
}

C# 动态添加类、动态添加类型、代码添加类型的更多相关文章

  1. JAVA“动态”为类添加属性

    部分参考:http://www.cnblogs.com/zy2009/p/6725843.html pom.xml中添加: <dependency> <groupId>comm ...

  2. python装饰器、继承、元类、mixin,四种給类动态添加类属性和方法的方式(一)

    介绍装饰器.继承.元类.mixin,四种給类动态添加类属性和方法的方式 有时候需要給类添加额外的东西,有些东西很频繁,每个类都需要,如果不想反复的复制粘贴到每个类,可以动态添加. # coding=u ...

  3. 使用TypeDescriptor给类动态添加Attribute

    给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overflow看了不少文章,算是最终有了答案. 先是有这样的一段解释 Attributes are stat ...

  4. 使用TypeDescriptor给类动态添加Attribute【转】

    源文 : http://www.cnblogs.com/bicker/p/3326763.html 给类动态添加Attribute一直是我想要解决的问题,从msdn里找了很久,到Stack Overf ...

  5. 01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类

    1==>控制字体图标的大小用 font-size:16px; <i class="el-icon-arrow-left right-show-aside-icon"&g ...

  6. Java动态生成类以及动态添加属性

    有个技术实现需求:动态生成类,其中类中的属性来自参数对象中的全部属性以及来自参数对象properties文件. 那么技术实现支持:使用CGLib代理. 具体的实现步骤: 1.配置Maven文件: &l ...

  7. C# 后台动态添加标签(span,div) 以及模板添加

    很多时候.我们需要在后台用C#代码添加html标签.而不是在html源码中添加. 比如在html源码中简单的一个input 标签 <input type="type" nam ...

  8. 秒懂C#通过Emit动态生成代码 C#使用Emit构造拦截器动态代理类

    秒懂C#通过Emit动态生成代码   首先需要声明一个程序集名称, 1 // specify a new assembly name 2 var assemblyName = new Assembly ...

  9. .net core3.1 abp动态菜单和动态权限(动态菜单实现和动态权限添加) (三)

    我们来创建动态菜单吧 首先,先对动态菜单的概念.操作.流程进行约束:1.Host和各个Tenant有自己的自定义菜单2.Host和各个Tenant的权限与自定义菜单相关联2.Tenant有一套默认的菜 ...

  10. 编写高质量代码改善C#程序的157个建议——建议137:委托和事件类型应添加上级后缀

    建议137:委托和事件类型应添加上级后缀 委托类型本身是一个类,考虑让派生类的名字以基类名字作为后缀.事件类型是一类特殊的委托,所以事件类型也遵循本建议. 委托和事件的正确的命名方式有: public ...

随机推荐

  1. Python学习笔记【第八篇】:Python内置模块

    什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...

  2. Python的简单介绍

    0. 前言 最近在从头梳理Python的相关知识,有助于以后更好地学习新知识.这篇博客,我简单介绍一下Python语言的有关内容. 1. Python介绍 Python的创始人为荷兰人吉多·范罗苏姆( ...

  3. 【PHP篇】字符串基础

    1.声明时既可以用双引号也可以用单引号 str1 =”字符串值”;    //可解析引号里的变量等内容 str2=’字符串值’;     //不可解析内容 2.字符串没有长度限制,但要注意内存的消耗 ...

  4. BeautifuSoup的使用

    BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单.

  5. SpringMvc参数传递中乱码问题

    问题描述: 当传递中文参数到controller类时,无乱是get方式还是post方式都出现乱码 解决: 1.保证所有的页面编码都是utf-8,包括jsp页面,浏览器编码设置和eclipse的编码设置 ...

  6. 【spring boot】idea下springboot打包成jar包和war包,并且可以在外部tomcat下运行访问到(转)

    转自:https://www.cnblogs.com/sxdcgaq8080/p/7727249.html   接着上一章走呗:http://www.cnblogs.com/sxdcgaq8080/p ...

  7. 微信小程序入门(二)

    5.安装微信开发者工具 小程序入口文档 点"小程序开发"-->>"工具-->>再点左边的"下载",进行开发者工具的下载 6.小 ...

  8. 如何配置React Native真机调试-iOS

    说在前面,本教程是建立在项目已经成功在模拟器上运行的基础上,如果你是还未配置好环境的新手,建议先从官网快速入门开始:官网英文版 . 中文版 ok, 切入正题,当你已经完成好环境配置,在模拟器上成功的运 ...

  9. Python3.7 dataclass 介绍

    Posted on 2018年6月28日 by laixintao 1 Comment Python3.7 加入了一个新的 module:dataclasses.可以简单的理解成“支持默认值.可以修改 ...

  10. Java集合:整体结构

    一.Java中集合 Java中集合类是Java编程中使用最频繁.最方便的类.集合类作为容器类可以存储任何类型的数据,当然也可以结合泛型存储指定的类型(不过泛型仅仅在编译期有效,运行时是会被擦除的).集 ...