使用 Reflection.Emit 创建一个实现接口的类 [英] Using Reflection.Emit to create a class implementing an interface
本文介绍了使用 Reflection.Emit 创建一个实现接口的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 Reflection.Emit 生成一个实现以下接口的类.
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);
}
}
推荐答案
如果您正在使用 Reflection.Emit,您真的应该获取一份 Reflection.Emit 语言插件,用于反射器.虽然并不完美,但它应该能让您至少完成 95% 的任何给定代码.
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屋!
查看全文