2011-12-08 25 views
0

是否有可能使一个RealProxy,看起来像这样:是否可以使用RealProxy通过Remoting公开类接口而不使用MarshalByRefObject?

public InterfaceProxy(object instance, params Type[] interfaces) 

,并可以公开所有这些interfaces了远程处理,委托给instance通话,即使一个实例是不是MarshalByRefObject

目前我有,代理接听电话时InitializeLifetimeService一个问题,无论我从它(包括null),我总是得到一个以下异常返回:

System.InvalidCastException:Return argument has an invalid type 
at System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg(Object arg, Type paramType) 
at System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParameters(IMessage msg, Object[] outArgs, Object returnValue) 
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnmessage(IMessage reqMsg, IMessage retMsg) 
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 

回答

3

只要有你的类实现IRemotingTypeInfo

伪代码如下所示:

class InterfaceProxy : RealProxy, IRemotingTypeInfo 
{ 
    // snip all the basics 

    bool IRemotingTypeInfo.CanCastTo(Type fromType, Object o) 
    { 
     return interfaces.Contains(fromType); 
    } 
} 
相关问题