0

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)?

Jak Erdy
  • 31
  • 2
  • 1
    I don't get it: why does `IsAssignableFrom` not work, I think it works no problem? – Charlieface Apr 18 '21 at 08:25
  • 1
    Here's a fiddle to prove https://dotnetfiddle.net/synCIC – Charlieface Apr 18 '21 at 08:35
  • It won't work for me because i don't have constructed generic type instances. But, i have list of all extension methods in it's generic form. My target is check, if it actually make sense to show some extension method is context of some specific type. For example, we have System.Enumerable with plenty of extensions. And some List. If i check just typeof(IEnumerable).IsAssignableFrom(List) it will return me false, because T != string. But, it is actually compatible, due to matching all type constraints and other stuff like that. – Jak Erdy Apr 18 '21 at 10:15
  • Finlay this was resolved, answer from Lazlo makes me really happy, because it just works, with little modifications. https://stackoverflow.com/a/41665993/10767112 – Jak Erdy Apr 21 '21 at 09:08

0 Answers0