Using reflection, I am trying to find a set of types that comes from a given base class Are there. It did not take long for simple types, but I was stumped while talking of generics.
For this piece of code, the first instance is correct, but the second return is false. And yet, the last assignment is just fine.
class class1 {} class class2: class1 {} class generic1 & lt; T & gt; {} Class Generic 2 & lt; T & gt; : Generic 1 & lt; T & gt; {} Class Program {Static Zero Main (string [] ARG) {Type C1 = Type 1 (Class 1); Type C2 = Type2 (Class 2); Console.lightline ("c1.IsAssignableFrom (c2): {0}", c1.IsAssignableFrom (c2)); Type g1 = typef (generic 1 <); Type g2 = typef (generic 2 <); Console.light line ("g1.IsAssignableFrom (g2): {0}", g1.IsAssignableFrom (g2)); Generic1 & lt; Class1 & gt; Cc = new generic2 & lt; Class1 & gt; (); }} So how do I determine at run time that a common type definition is taken from the other?
From:
Public Stable Bull. Foreign (in this interface type) {if (Generic type & amp; amp; .GetGenericTypeDefinition () == Generic type) returns true; } If (given type. Eigenic type & amp; amp; typed.getGenericTypeDefinition () == generic type) back true; Type the basetty = given type Base type; If (base type == zero) return false; Return IsAssignableToGenericType (BaseType, Generic Type); }
(If you prefer the answer, then let the answer related to the code be raised.)
Comments
Post a Comment