0

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
  } 
monty
  • 7,888
  • 16
  • 63
  • 100
  • I'm not getting why it has to be a *string* specifically. I'm not getting what the relationship is between the two libraries. Why can it not, at the least, pass it as a *Type*? – Damien_The_Unbeliever Apr 09 '19 at 07:00
  • Well, you can do using reflection. However be aware that when you provide the actual type at **runtime** there´s no way for the **compiler** to infer the right type. Therefor all you get at compile-time is an `object`, instead of `IEnumerable`. – MakePeaceGreatAgain Apr 09 '19 at 07:03
  • Having read the question, and still not clear what the problem is (please provide more details), I submit this for your consideration: `Select(x => x.ToString())`. – Dialecticus Apr 09 '19 at 07:10
  • Your code can use `.OfType().Any()` instead of a specific type. – Enigmativity Apr 09 '19 at 07:15
  • wouldn't .OfType().Any() be true if it is annotated with any attribute? Don't I need to filter for the specific pagination annotation? – monty Apr 09 '19 at 07:17
  • Maybe `Any(x => x.GetType().Name == "MyTypeString")`? – Dialecticus Apr 09 '19 at 07:20
  • @Enigmativity Please give me a hint, which of the 7 questions from the duplicate solves my problem? – monty Apr 09 '19 at 07:20
  • @monty - Yes, sorry. You would need to filter by type - but you can do that as a `string` as Dialecticus suggests. – Enigmativity Apr 09 '19 at 07:22
  • @monty - The accepted answer tells you how to call generic methods when you only have the type at run-time. – Enigmativity Apr 09 '19 at 07:23
  • May it's me but none of the answers work (at least not in that one case). I editet the question to reflect that one case. Mind removing the duplicated flag? – monty Apr 09 '19 at 07:44

0 Answers0