Linq offers a method to filter an enumerable for a given type.
public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source);
But this means I need to now the type (and have access to it) at compile time. In order to let someone define the type as string how would I call this having the type only as string?
Sure, I could write my own function (since the implementation isn't that heavy).
I am just curious if there is a way to call without writing my own method. The compiler complains on everything I tried so far.
In my case I have one lib defining an attribute and a second lib allowing to something if the attribute is set. But the filtering library does not know the first library, so I would need to pass this as string.
Well, in detail I am trying to write a Swagger/Swashbuckle operation filter adding the infos for typical pagination. Unfortunatly the pagination itself is in an (replaceable) pagination library. So I need to pass the information which attribute defines that this methods supports pagination from the pagination library to the Swagger/Swaschbuckle library (or define it as value in the configuration).
The code itself does only check if the attribute is set (thats all where I need the type).
public void Apply(Operation operation, OperationFilterContext context)
{
bool paginationSupported = context.ControllerActionDescriptor
.GetControllerAndActionAttributes(inherit: true)
.OfType<MyTypeAsString>() //???
.Any();
if (paginationSupported) // do stuff
}