使用Reflection.Emit的创建实现接口的类 [英] Using Reflection.Emit to create a class implementing an interface

查看:226
本文介绍了使用Reflection.Emit的创建实现接口的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成使用Reflection.Emit的实现以下接口的类。

 公共接口IObject 
{
吨得到< T>(字符串propertyName的);
}



有没有人有我怎么会发出以下内容作为一个简单的测试示例?案例

 类GeneratedObject:IObject 
{
公众吨得到< T>(字符串propertyName的)
{
//这是最简单的可能的实现
返回缺省值(T);
}
}


解决方案

如果您正在使用Reflection.Emit的,你真的应该抢 Reflection.Emit的语言加载项以反射。虽然并不完美,它应该让你的方式,任何发出的代码至少95%。


I need to generate a class using Reflection.Emit that implements the following interface.

public interface IObject
{
    T Get<T>(string propertyName); 
}

Does anyone have an example of how I would emit the following as a simple test case?

class GeneratedObject : IObject
{
    public T Get<T>(string propertyName)
    {
        // this is the simplest possible implementation
        return default(T);
    }
}

解决方案

If you're using Reflection.Emit, you really ought to grab a copy of the Reflection.Emit language add-in for Reflector. While not perfect, it should get you at least 95% of the way to any given emitted code.

这篇关于使用Reflection.Emit的创建实现接口的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆