我有一种情况,我需要确定一个继承类是否是一个特定的继承类,但模型中预期的类型是基类,它是使用nHibernate
/Fluent nHibernate
使用Table Per Concrete Class
层次结构存储的。所以我的结构看起来有点像这样..
class Mutation : Entity {
virtual Aspect Aspect { get; set; }
virtual Measurement Measurement { get; set; }
}
abstract class Measurement : Entity {
// does nothing on its own, really.
}
class Numeric : Measurement {
virtual int Value { get; set; }
// may have some other properties
}
class Observable : Measurement {
virtual Aspect Aspect { get; set; }
}
所以基本上,这里发生的事情就是这样。Mutation
期望指向一种数据类型和测量的变化(可能有其他方法来更改数据,而不仅仅是平面数字)。所以我会有一个对象,它只需要一个IList<Mutation>
并将每个后续类型映射Measurement
到它自己的特定表,Identity
与基Measurement
类共享。到目前为止效果很好。
现在 anObservable
的不同之处在于它不存储自己的值,而是再次指向另一个Aspect
可能具有自己的一组突变和变化的值。这个想法是该值将始终从预期的源中检索,而不是作为平面值保存在数据库中。(想要这种行为是有原因的,超出了这个问题的范围)
那么,我的想法基本上是进行这样的评估..
foreach(var measurement in list) {
if(measurement is Observable){
// then we know to lookup the other value
}
}
那没有用。我仍然得到 just 的代理结果MeasurementProxy
。但是相同的代码在C#
不使用 nHibernate 的独立应用程序中也能正常工作,所以我非常有信心认为问题出在代理上。
然后我将以下方法添加到我的基Entity
类中......
/// <summary>
/// Unwrap the type from whatever proxy it may be
/// behind, returning the actual .NET <typeparamref name="System.Type"/>.
/// </summary>
/// <returns>
/// A pure <typeparamref name="System.Type"/> that is no longer proxied.
/// </returns>
public virtual Type Unwrap() {
return GetType();
}
现在,如果我这样做,Console.WriteLine(measurement.Unwrap());
我会得到正确的类型,但同样的评价......
foreach(var measurement in list) {
if(measurement.Unwrap() is Observable){
// then we know to lookup the other value
}
}
仍然无法正常工作。它从不运行。有谁可以帮我离开这里吗?