在这些给定的课程
[ActiveRecord]
public class BaseMoo : ActiveRecordBase
[ActiveRecord]
public class Foo : BaseMoo { }
我在代码中的某处
var fooObj = new Foo();
// do something with fooObj
BaseMoo obj = fooObj;
这里我试图施展
var newFooObj = (Foo)obj; // and goes well
如果我将类更改为:
[ActiveRecord(Lazy=true)]
public class BaseMoo : ActiveRecordBase
[ActiveRecord(Lazy=true)]
public class Foo : BaseMoo { }
在这些方面成为Lazyest:
var fooObj = new Foo();
// do something with fooObj
BaseMoo obj = fooObj;
AR创建非Foo实例而不是BaseMooProxy对象
var newFooObj = (Foo)obj; // this throws an invalidcast exception
由@Adam Houldsworth在this question
中消化我以这种方式重载Foo显式运算符
public static explicit operator Foo(NHibernate.Proxy.INHibernateProxy nhProxy)
{
var resultObj = (Foo)nhProxy.InSomeWayIGetTheOriginalObject(); // InSomeWayIGetTheOriginalObject() is not a real method, only for simplify
return resultObj;
}
但我在运算符重载参数
中出错NHibernate.Proxy.INHibernateProxy nhProxy
user-defined conversion from interface
问题:
如何从BaseMooProxy(NHibernate.Proxy.INHibernateProxy)显式转换为Foo?
因为知道不可能重载“is”运算符,有没有办法让“obj is Foo”工作?
由于消费者应用程序是第3部分客户,因此无法更改应用程序转换类对象的方式。
提前感谢。