对原始类型的透明代理
我有一个类型为 {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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过调用
MarshalServices.GetRealProxy()
,但是获取服务器对象引用会更困难,因为默认的RealProxy
只有非公共成员公开此引用(受保护的方法GetUnwrappedServer()
和内部属性UnwrappedServerObject
)。如果 RealProxy 是由您自己实现或通过反射实现(如果您有足够的信任来执行此操作),您可以获取这些内容。You can get the
RealProxy
instance for the transarent proxy by callingMarshalServices.GetRealProxy()
, but getting the server object reference is then harder because the defaultRealProxy
only has non-public members exposing this reference (a protected methodGetUnwrappedServer()
and an internal propertyUnwrappedServerObject
). You can either get to those if theRealProxy
is implemented by yourself or via reflection (if you have enough trust to perform this).您不应该获得对 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)