问题描述
我有一个类型为{System.Runtime.Remoting.Proxies .__ TransparentProxy}的运行时对象,该对象是从ContextBoundObject继承的类的实例创建的.该类向其他对象引发事件,我需要将此代理对象转换为原始对象.所有对象都在单个系统上的默认AppDomain中.
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
推荐答案
您可以通过调用 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).
这篇关于原始类型的透明代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!