Ric*_*ard 10 c# generics nhibernate
我有一段代码需要在我的存储库保存时检查实体.我在保存上有一个NHibernate拦截器来检查这个但是当我调用该GetGenericTypeDefinition函数时代码失败并出现错误:
[InvalidOperationException:由于对象的当前状态,操作无效.] System.RuntimeType.GetGenericTypeDefinition()+ 7525641
代码是这样的:
protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
{
if (evt.Entity.GetType().GetGenericTypeDefinition() == typeof(IChild<>))
{
var parent = (evt.Entity as IChild<Entity>).Parent;
if (parent != null)
{
parent.UpdateCacheCounters();
evt.Session.Save(parent);
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
And*_*rey 14
Type type = evt.Entity.GetType();
if(
type.IsGenericType &&
type.GetGenericTypeDefinition() == typeof(IChild<>)
)
Run Code Online (Sandbox Code Playgroud)
试试这个.根据http://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition.aspx:
InvalidOperationException:当前类型不是泛型类型.也就是说,IsGenericType返回false.