I have a generic method (see code below) and want to perform some action if T is a particular interface.
Obviously I could just do a typeof(T).IsAssignableFrom(IFoo) but reflection is comparatively slow so I want to avoid it if possible.
Now, I know I can’t have another method with the a constraint because I’d get the ambiguous method error otherwise I’d just do that.
Is there a way of doing what I want?
Does the fact that I want to do this point to some architectural problem?
public T Load<T>(string name)
{
T result = LoadFromName<T>(name);
if(T is IFoo) // Obviously doesn't work as T is a type, not a variable
(result as IFoo).FooMethod();
return result;
}
But you have got a variable –
result! What’s wrong with