Is there a way to determine if a generic type is built from a specific generic type definition?

后端 未结 4 1360
忘掉有多难
忘掉有多难 2020-12-18 08:45

I\'ve got a generic method:

Func, bool> CreateFunction()

where T can be any number of diff

4条回答
  •  余生分开走
    2020-12-18 09:28

    I think that if you call Type.GetGenericTypeDefinition() that should return the "base" generic type used to construct the concrete Type.

    Note that just comparing this to IDictionary<,> is likely not enough, because if someone passes in an instance of Dictionary<,> I assume you would want to use that, as well. You could either check to see if the Type implements IDictionary<,> or you might be able to call Type.IsAssignableFrom(), although based on the doc I'm not sure how well this would work with generic Types.

提交回复
热议问题