开发者

Why does GetGenericTypeDefinition fail?

开发者 https://www.devze.com 2023-01-20 15:56 出处:网络
I have a piece of code which needs to check an entity when being saved by my Repository. I have an NHibernate interceptor on the save to check this but when I call the GetGenericTypeDefinition functio

I have a piece of code which needs to check an entity when being saved by my Repository. I have an NHibernate interceptor on the save to check this but when I call the GetGenericTypeDefinition function the code fails with the error:

[InvalidOperationException: Operation is not valid due to the current state of the object.] System.RuntimeType.GetGenericTypeDefinition() +7525641

Code is this:

protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
{
    if (evt.Entit开发者_StackOverflow中文版y.GetType().GetGenericTypeDefinition() == typeof(IChild<>))
    {
        var parent = (evt.Entity as IChild<Entity>).Parent;
        if (parent != null)
        {
            parent.UpdateCacheCounters();
            evt.Session.Save(parent);
        }
    }
}

Any help would be much appreciated.


Type type = evt.Entity.GetType();
if(
    type.IsGenericType && 
    type.GetGenericTypeDefinition() == typeof(IChild<>)
)

try this. according to http://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition.aspx:

InvalidOperationException: The current type is not a generic type. That is, IsGenericType returns false.

0

精彩评论

暂无评论...
验证码 换一张
取 消