If i have some class like this
interface IDeclaration<T> {...}
how should i implement check if instance of my particular class derived from IDeclaration<T>
and if so - what is the type T
in our particular case.
Thank you in advance
If i have some class like this
interface IDeclaration<T> {...}
how should i implement check if instance of my particular class derived from IDeclaration<T>
and if so - what is the type T
in our particular case.
Thank you in advance
IDeclaration
is an interface, not a class. Classes can implement this interface, not derive from it.
To check whether a particular object implements the IDeclaration<T>
interface, you could call the GetInterfaces
method on that instance type and search for that particular interface like this:
var instance = ...
var @interface =
instance.GetType()
.GetInterfaces()
.FirstOrDefault(i =>
i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof (IDeclaration<>));
bool is_IDeclaration = @interface != null;
To get the generic type parameter (T
), you can use the GetGenericArguments
method like this:
if (is_IDeclaration)
{
var typeof_T = @interface.GetGenericArguments()[0];
}