What is the simplest way of testing if an object implements a given interface in C#? (Answer to this question in Java)
I had a situation where I was passing a variable to a method and wasn't sure if it was going to be an interface or an object.
The goals were:
I achieved this with the following:
if(!typeof(T).IsClass)
{
// If your constructor needs arguments...
object[] args = new object[] { my_constructor_param };
return (T)Activator.CreateInstance(typeof(T), args, null);
}
else
return default(T);