To check if your instance is a type MyGenericClass<T>, you can write something like this.
MyGenericClass<string> myClass = new MyGenericClass<string>();
bool b = myClass.GetType().GetGenericTypeDefinition() == typeof(MyGenericClass<>);
, MyGenericClass MyGenericClass<string>, MyGenericClass. / , . . *
* , ,
var myClass = new MyGenericClass<string>();
: , class Foo : MyGenericClass<string>. Foo MyGenericClass<>, .
Func<object, bool> isMyGenericClassInstance = obj =>
{
if (obj == null)
return false;
Type t = obj.GetType().BaseType;
if (t != null)
{
if (t.IsGenericType)
return t.GetGenericTypeDefinition() == typeof(MyGenericClass<>);
}
return false;
};
bool willBeTrue = isMyGenericClassInstance(new Foo());
bool willBeFalse = isMyGenericClassInstance("foo");