I have a generic function and I want to check whether the type parameter is an interface. Is there anyway to do that? Thanks in advance!
Use the IsInterface
property of Type
..
public void DoCoolStuff<T>()
{
if(typeof(T).IsInterface)
{
//TODO: Cool stuff...
}
}
If you want to constrain your generic method so that the type parameter can only be a type that implements some specific interface and nothing else, then you should do the following:
void YourGenericMethod<T>() where T : IYourInterface {
// Do stuff. T is IYourInterface.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With