惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - rex

C#编译javascript - JQuery模板 将C#编译为javascript TouchPad App - Advanced Calculator TouchPad App - Unit converter webOS App - Simple Unit Convert 在javascript库中整合Sizzle(Sizzle引擎的使用) source code upload to google code RexStudio2.0RC c# 2.0 Factory的实现 C#2.0 Singleton 的实现 .NET 分布式事务处理 Castle IoC castle 五子棋:) *.ashx:HttpHandler的另一种用法 自己制作的ORMap框架终于完成的差不多了。 VStudio 2003 remote debug Application Container AOP static crosscutting 的一个实现
Emit生成Property
rex · 2006-04-09 · via 博客园 - rex

最近在ORM的实现当中碰到一个新的需求:映射一个接口或者抽象类. 问题可以简单描述为:为一个接口或抽象类生成属性的具体实现,据个例子:
有接口如下

public interface IPerson
{
    int Age{get;set;}
    string Name{get;set;}
}

那么我们需要动态的生成以下这样一个类

public class Impl_Person:IPerson
{
    protected int m_Age;
    public int Age
    {
        get{return m_Age;}
        set{m_Age=value;}
    }

    protected int m_Name;
    public string Name
    {
        get{return m_Name;}
        set{m_Name=value;}
    }
}

解决方法有很多,我选择用Emit实现,关于Emit,网上简单的例子非常多,我只把重要的一些代码列出来:
PropertyInfo propInfo;//假设已经用反射获得某个porroperty的属性
TypeBuilder typeBuilder;//假设前面代码已经定义

//构造私有/保护成员(field)
FieldBuilder fieldBuilder = typeBuilder.DefineField("m_"+ propInfo.Name, propInfo.PropertyType, FieldAttributes.Private);

//开始构造property
PropertyBuilder propBuilder = typeBuilder.DefineProperty(propInfo.Name, PropertyAttributes.HasDefault, propInfo.PropertyType, null);

//get,set的属性(在这里可以看到get和set在.NET里面其实是一种特殊的method)
MethodAttributes methodAttribute = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MehodAttributes.Virtual;

//生成方法构造器
MethodBuilder getMethodBuilder = typeBuilder.DefineMethod("get_"+ propInfo.Name, methodAttribute, propInfo.PropertyType, Type.EmptyTypes);

                ILGenerator getILGen = getMethodBuilder.GetILGenerator();

                //以下3行是IL,是get_XXX方法里面的具体代码
                getILGen.Emit(OpCodes.Ldarg_0);
                getILGen.Emit(OpCodes.Ldfld, fieldBuilder);
                getILGen.Emit(OpCodes.Ret);

                propBuilder.SetGetMethod(getMethodBuilder);//不要遗漏这句哦,普通的method是不需要这句的

MethodBuilder setMethodBuilder = typeBuilder.DefineMethod("set_" + propInfo.Name, methodAttribute, null, new Type[] { propInfo.PropertyType });

                ILGenerator setILGen = setMethodBuilder.GetILGenerator();

                //以下四行是IL,是set_XXX方法里面的具体代码
                setILGen.Emit(OpCodes.Ldarg_0);
                setILGen.Emit(OpCodes.Ldarg_1);
                setILGen.Emit(OpCodes.Stfld, fieldBuilder);
                setILGen.Emit(OpCodes.Ret);

                propBuilder.SetSetMethod(setMethodBuilder);