Giter VIP home page Giter VIP logo

Comments (4)

impworks avatar impworks commented on June 24, 2024

С точки зрения кода на CIL, вызов obj.SomeExtMethod(param1) является синтаксическим сахаром для StaticClass.SomeExtMethod(obj, param1). С точки зрения того, как найти подходящий помеченный метод - пока не знаю, но не думаю, что с этим будет какая-то сверхъестественная проблема.

from lens.

impworks avatar impworks commented on June 24, 2024

Для того, чтобы найти Extension-методы, можно сделать следующее:

  1. Взять список пространств имен, которые были указаны через using.
  2. Пройтись по классам сборок и найти типы, которые входят в эти пространства имен.
  3. Типы, в которых есть Extension-методы, помечены ExtensionAttribute и имеют модификатор public static sealed.
  4. Сами Extension-методы также помечены ExtensionAttribute, а их релевантность можно определить по типу первого аргумента.

from lens.

impworks avatar impworks commented on June 24, 2024
var searchType = typeof(IEnumerable<>);
var searchName = "Any";

var types = typeof(System.Linq.Enumerable).Assembly.GetTypes();
var extMeths = from type in types
           where type.IsSealed && !type.IsNested && !type.IsGenericType
           where type.IsDefined(typeof(ExtensionAttribute), false)
           from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
           where method.IsDefined(typeof(ExtensionAttribute), false)
           select new { Method = method, ArgType = method.GetParameters()[0].ParameterType };

var applying = from exm in extMeths
            where exm.ArgType == searchType || (exm.ArgType.IsGenericType && exm.ArgType.GetGenericTypeDefinition() == searchType)
            where exm.Method.Name == searchName
            select exm.Method;
applying.Dump();

from lens.

impworks avatar impworks commented on June 24, 2024

Запилено в виде класса ExtensionMethodResolver.

from lens.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.