We just released Supurlative and one of my favorite features is Formatters. It gives you the ability to intercept any type and perform custom logic. In our current API work, we wanted to intercept a type that implemented a generic interface. That type is an
To check whether the type we come across is the
IRange<T> interface, we use the following code:
public override bool IsMatch(Type currentType, SupurlativeOptions options)
&& currentType.GetGenericTypeDefinition() == typeof(IRange<>);
We need to do two boolean checks:
- Is the current type Generic?
- Is the GenericTypeDefinition equal to our generic interface?
That's all it takes to do this kind of check.
Note: Credit to Bill Boga for figuring this out.