jeudi 14 avril 2022

C# how to check if generic arguement is a real type?

Hello,

I have a code like the following:

IEnumerable<Type> types = new []
{
    typeof(MyService),
    typeof(ILogger<MyService>),
    typeof(ILogger<>),           // <-- that are the interesting lines
    typeof(IDictionary<,>)       // <-- that are the interesting lines
};

Now I want to filter all types, where the generic argument is not set

types
    .Where(t => t.IsGenericType is false || t.GetGenericArguments.All(at => /* is argument type a real type? */))
    .ToArray();

As shown in the example I have troubles to figure out if a type is just a placeholder or it is a real type.
I found it could work by proofing string.IsNullOrEmpty(a.FullName) is false but I think it´s not a really beautiful solution.
There must be something better. đŸ€ž

Thank you for your help!





Aucun commentaire:

Enregistrer un commentaire