是否可以通过 DynamicProxy 将属性添加到类型?
我使用 Castle DynamicProxy 在运行时创建给定类型的代理 - 包括几个 mixin。
我试图弄清楚是否也可以向代理添加任意属性,例如:
class BaseType
{
string Foo { get; set; }
}
在运行时,我创建一个新类型,如下所示:
class BaseTypeProxy3848484etc
{
string Foo { get; set; }
OtherType Bar { get; set; }
}
理论上,这似乎应该 有可能——也许我只是不知道如何用 Castle 来做这件事......有什么想法吗?谢谢!
I'm using Castle DynamicProxy to create a proxy of a given type at runtime - including a couple mixins.
I'm trying to figure out if it's possible to also add arbitrary properties to the proxy, e.g.:
class BaseType
{
string Foo { get; set; }
}
and at runtime, I create a new type, that would look like this:
class BaseTypeProxy3848484etc
{
string Foo { get; set; }
OtherType Bar { get; set; }
}
In theory, it seems like this should be possible-- maybe I'm just not seeing how to do it with Castle... Any thoughts? Thanks!
扩展 DynamicObject,您可以创建实例的代理并向要添加的属性添加行为。
Extending DynamicObject you can create a proxy of your instance and add behaviour to the properties you want to add.