I have an interface that looks something like this:
public interface IFoo<Twhere T : IFoo<T{ ... }
Inside a class that looks something like this...
public class Bar<V{ ... }
....I would like to find out if V implements IFoo<V>. However, I can't
do this...
if (typeof(IFoo<V>).IsAssignableFrom(typeof(V))) { ... }
....because it's circular: the compiler won't let us refer to IFoo<V>
because we don't know if V implements IFoo<V>. The best I can do is
this:
bool implementsInterface = true;
try
{
typeof(IFoo<>).MakeGenericType(typeof(V));
}
catch (ArgumentException)
{
// Oops! I guess not.
implementsInterface = false;
}
However, I don't get the impression this is the "right" way to do it.
I don't like the idea of catching an ArgumentException in general. Is
there some other way to do this?