是否可以通过 DynamicProxy 将属性添加到类型?

发布于 2024-09-12 05:17:27 字数 391 浏览 1 评论 0原文

我使用 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-09-19 05:17:27

扩展 DynamicObject,您可以创建实例的代理并向要添加的属性添加行为。

Extending DynamicObject you can create a proxy of your instance and add behaviour to the properties you want to add.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文