Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using IsAssignableFrom with 'open' generic types

Using reflection, I'm attempting to find the set of types which inherit from a given base class. It didn't take long to figure out for simple types, but I'm stumped when it comes to generics.

For this piece of code, the first IsAssignableFrom returns true, but the second returns false. And yet, the final assignment compiles just fine.

class class1 { } class class2 : class1 { } class generic1<T> { } class generic2<T> : generic1<T> { }  class Program {     static void Main(string[] args)     {         Type c1 = typeof(class1);         Type c2 = typeof(class2);         Console.WriteLine("c1.IsAssignableFrom(c2): {0}", c1.IsAssignableFrom(c2));          Type g1 = typeof(generic1<>);         Type g2 = typeof(generic2<>);         Console.WriteLine("g1.IsAssignableFrom(g2): {0}", g1.IsAssignableFrom(g2));          generic1<class1> cc = new generic2<class1>();     } } 

So how do I determine at run time whether one generic type definition is derived from another?

like image 484
ThatBlairGuy Avatar asked Mar 28 '11 15:03

ThatBlairGuy


People also ask

What is an open generic type?

An open generic type is simply a generic type whose type parameters have not been specified. For example, IEnumerable<> is an open generic type, and IEnumerable (or string or whatever) is a **closed generic type**, as its type parameter has been specified.

When would you use a generic interface?

It's often useful to define interfaces either for generic collection classes, or for the generic classes that represent items in the collection. To avoid boxing and unboxing operations on value types, it's better to use generic interfaces, such as IComparable<T>, on generic classes.

What is open generic C#?

The C# language defines an open type to be a type that's either a type argument or a generic type defined with unknown type arguments: All types can be classified as either open types or closed types. An open type is a type that involves type parameters. More specifically: A type parameter defines an open type.

Can a generic be an interface?

Java Generic Classes and SubtypingWe can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.


1 Answers

From the answer to another question:

public static bool IsAssignableToGenericType(Type givenType, Type genericType) {     var interfaceTypes = givenType.GetInterfaces();      foreach (var it in interfaceTypes)     {         if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)             return true;     }      if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)         return true;      Type baseType = givenType.BaseType;     if (baseType == null) return false;      return IsAssignableToGenericType(baseType, genericType); } 

(If you like the answer please upvote the linked answer since the code isn’t mine.)

like image 127
3 revs Avatar answered Sep 21 '22 15:09

3 revs