dimanche 18 avril 2021

Robust way to check via reflection if extension method could be invoked on specific type

Is there any ready to use solution to check (via reflection) if this arg of extension method is compatible with a specific arbitrary type?

In cases where extension methods has simple types on this parameter, it is a trivial task, you could just invoke Type.IsAssignableTo(), and you done.

But, if this arg is something like this:

public static void ExtMethod<T, V, X>(this Dictionary<V, List<string>> arg)
    where V : IDictionary<List<X>, T>, new()
    where T : class, IDisposable

Such checking could become pretty hard to resolve. Of course my example doesn't look practical, but anything possible in this world, and tool which i am trying to make should be able to handle any ridiculous but valid (from C# point of view) constructions.

Task context

I am currently developing smart auto completion in .net oriented IDE-like tool. Something like intelli-sense in VS. And i need to provide user with extension methods which could be used on type which is in current context of completion popup. But, how to find all extension methods for this type, make me a good trouble, because i couldn't even see the way to build such matching algorithm.

I wonder if there any alternative ways to do this (with Cecil for example)?





Aucun commentaire:

Enregistrer un commentaire