是否可以确定可用于强制转换MarshalByRefObject对象的接口?
cast操作符如何处理MarshalByRefObject对象?它会调用CreateObjRef方法吗?
谢谢,马西莫
发布于 2011-12-19 23:47:42
以下是可用于检索接口列表的变通方法。
定义公共接口IDescriptor
public interface IDescriptor
{
List<string> GetInterfaces();
}定义实现接口的基类:
public class BaseMasrhalByRefObject : MasrhalByRefObject, IDescriptor
{
public BaseMasrhalByRefObject() : base() {}
public List<string> GetInterfaces()
{
List<string> types = new List<string>();
foreach(Type i in GetType().GetInterfaces())
{
types.Add(i.AssemblyQualifiedName);
}
return types;
}
}然后使用BaseMasrhalByRefObject而不是MasrhalByRefObject来定义服务对象:
public class MyServiceObject : BaseMasrhalByRefObject, MyInterface1, MyInterface2, ...
{
// Add logic method
}在AppDomain A中创建MyServiceObject的引用对象。在AppDomain B中,获取远程对象的代理。可以将代理转换为IDescriptor:
public List<Type> GetInterfaces(MasrhalByRefObject proxy)
{
List<Type> types = new List<Type>();
IDescriptor des = proxy as IDescriptor;
if (des != null)
{
foreach(string t in des.GetInterfaces()) // this is a remote call
{
types.Add(Type.GetType(t);
}
}
return types;
}发布于 2011-12-17 01:11:34
MarshalByRefObject是一个类,所以只有一个接口,你不能确定是否所有实现它的类都是从MarshalByRefObject派生的。但是,如果您有一个对象的实例,则可以使用以下表达式轻松地进行检查:
if (obj1 is MarshalByRefObject)
{
// do your thing
}https://stackoverflow.com/questions/8537315
复制相似问题