对原始类型的透明代理

发布于 2024-11-29 23:21:13 字数 625 浏览 6 评论 0原文

我有一个类型为 {System.Runtime.Remoting.Proxies.__TransparentProxy} 的运行时对象,它是从继承自 ContextBoundObject 的类实例创建的。此类向其他对象引发事件,我需要将此代理对象转换为原始对象。所有对象都位于单个系统上的默认 AppDomain 中。

public abstract class ObjectBase : ContextBoundObject, IObjectBase
{
}

public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next)
        {
            _context = (ObjectBase)o;// as ObjectBase; does not give any error but type remains 
/// transparent proxy in VS watch window.
// no property to get the  underlying type of the proxy
             return _aspect;
        }

如何将它们转换为原始对象?如果在相同的内存上运行,为什么要创建代理

I have an run time object of type {System.Runtime.Remoting.Proxies.__TransparentProxy} which is created from an instance of class which is inherited from ContextBoundObject. This class raise an event to some other object, I need to convert this proxy object to original object. All objects are in default AppDomain on single system.

public abstract class ObjectBase : ContextBoundObject, IObjectBase
{
}

public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next)
        {
            _context = (ObjectBase)o;// as ObjectBase; does not give any error but type remains 
/// transparent proxy in VS watch window.
// no property to get the  underlying type of the proxy
             return _aspect;
        }

How to convert them into original object? Why proxy is cretaed if running on same memory

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

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

发布评论

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

评论(2

爱人如己 2024-12-06 23:21:13

您可以通过调用 MarshalServices.GetRealProxy(),但是获取服务器对象引用会更困难,因为默认的 RealProxy只有非公共成员公开此引用(受保护的方法 GetUnwrappedServer() 和内部属性 UnwrappedServerObject)。如果 RealProxy 是由您自己实现或通过反射实现(如果您有足够的信任来执行此操作),您可以获取这些内容。

You can get the RealProxy instance for the transarent proxy by calling MarshalServices.GetRealProxy(), but getting the server object reference is then harder because the default RealProxy only has non-public members exposing this reference (a protected method GetUnwrappedServer() and an internal property UnwrappedServerObject). You can either get to those if the RealProxy is implemented by yourself or via reflection (if you have enough trust to perform this).

安稳善良 2024-12-06 23:21:13

您不应该获得对 Context 绑定对象的实际引用。 Evan,你使用反射/内部 API 获取引用,你会得到意想不到的行为(因为你违反了规则)。
您可以使用谷歌获得有关上下文对象的更多内部信息。

我认为您的实际架构/设计存在问题。你不能让一个对象同时“敏捷”和“上下文绑定”。一种解决方案是将大对象分成两部分(一个是上下文绑定的,另一个是敏捷的,并在它们之间保存引用)。

因此,当您将“敏捷”对象(继承自 MArshallByRefObject)的引用获取到创建的 AppDomain 中时,您将获得真实的对象引用,而不是代理。 (这是 MarshallByRefObject 定义)

You shouldn't get the actual reference to an Context bound object. Evan you get the reference using reflection/internal API you will get unexpected behavior (cause you break the rules).
You can get more insides about context object using google.

I think you have an issue in your actual architecture/design. You can not have an object to be "agile" and "context bound" at the same time. A solution is to split your big object in 2 (one context bound and another agile, and hold a reference(s) between them).

So when you get the reference of the "agile" one (which inherits from MArshallByRefObject) into the creation AppDomain you get the real object reference, not a proxy. (this is the MarshallByRefObject definition)

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