拦截动态属性访问器 .NET 4

发布于 2024-11-28 17:31:23 字数 249 浏览 4 评论 0原文

这是一个有趣的问题(至少对我来说):是否可以拦截对象中的任意属性访问器(getter/setter),以便我可以将该请求重新路由到其他地方,例如私有成员对象?

如果我能让它看起来好像我的外部对象自动可以访问调用者不知道的内部对象的某些内部属性(即除了少数例外之外的所有内容),我会很高兴,实际上使外部对象“shell”对象的行为与内部对象类似,无需手动创建映射到每个内部对象属性的属性。

可能有一个模式,但我的猜测是它涉及手动将外部属性连接到内部属性。

Here's an interesting question (to me at least): Is it possible to intercept arbitrary property accessors (getters/setters) in an object, such that I can reroute that request elsewhere, such as to a private member object?

I'd love it if I could make it appear as though my outer object automatically has access to certain inner properties (i.e. everything except for a few exceptions) of an inner object that the caller doesn't know about, in effect making the outer "shell" object behave like the inner object, without having to manually create properties that map to each of the inner object's properties.

There's probably a pattern for that, but my guess is it involves manually wiring the outer properties to the inner properties.

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

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

发布评论

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

评论(2

新人笑 2024-12-05 17:31:23

我的开源框架 ImpromptuInterface(在 nuget 上)有很多工具供您使用这些事情。它的主要功能是生成代理,使用 dlr 实现和连接并转发呼叫。

它还具有许多函数,使 dlr 调用能够以交互方式访问。还有一些 DynamicObjects 示例,为制作有用的动态对象提供了起点就像即席转发器

My open source framework ImpromptuInterface (on nuget) has a lot of tools for you to do these kind of things. It's main feature is than it generates proxy's that implement and interface and forward the calls using the dlr.

It's also got a lot of functions that make dlr calls interactively accessible. And some examples of DynamicObjects that give a starting place for making useful dynamic objects like ImpromptuForwarder.

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