c# - All extensions methods for type string -


how methods "string" type, extensions methods "aggregate", "select" , other c# reflection? know type implement interface ienumerable<>, extension methods in enumerable class first generic parametr tsource.ok... code:

var type = typeof(string).getmethods(); //i methods string type //i want type extensions methods "select" "where" //so interfaces  type.getinterfaces(); //icompareble //icloneable //... //ienumearable, interfaces don't have extensions  methods //they locate in enumerable class  //how can use string type go enumerable class , methods //somthigs typeof(enumerable).getmethods(); //i want methods using type "string" //aggregate //select //where 

extension methods can of course defined in different assemblies, first question assemblies care about. we'll start with

var assemblies = gettype().assembly     .getreferencedassemblies()     .select(an => assembly.load(an))     .concat(enumerable.repeat(gettype().assembly, 1)); 

to (in context of instance method or property) current assembly , references, feasible source extension methods available there , then. other uses have other starting points.

now need extension methods:

var availableextensionmethods = assemblies     // first types     .selectmany(asse => asse.getexportedtypes())     // cut out cannot static classes first     .where(t => t.isabstract && t.issealed && t.getconstructors().length == 0)     // methods.     .selectmany(t => t.getmethods())     // restrict extension methods     .where(m => m.getcustomattributes().any(ca => ca system.runtime.compilerservices.extensionattribute)     // extension method must have @ least 1 parameter, we'll rule out being     // messed strangely defined method through weird direct use of     // extensionattribute attribute     && m.getparameters().length != 0)     // object method , first parameter we'll use below.     .select(m => new {method = m, firstparam = m.getparameters()[0]}); 

now, defined directly in terms of string (somemethod(this string arg)) on base class (somemethod(this object arg)) be:

var stringextensions = availableextensionmethods     .where(info => info.firstparam.parametertype.isassignablefrom(typeof(string)))     .select(info => info.method); 

the above include (this ienumerable<char> arg). defined generically on generic type string implements (e.g (this ienumerable<t> arg) use:

var stringgenericinterfaces = typeof(string).getinterfaces()     .where(i => i.isgenerictype)     .select(i => i.getgenerictypedefinition());     var extensionsongenericinterfaces = info in         availableextensionmethods.where(aem => aem.firstparam.parametertype.containsgenericparameters)         inter in stringgenericinterfaces         info.firstparam.parametertype.getgenerictypedefinition().isassignablefrom(inter)         select info.method; 

you can union these lot.

i haven't included checks on constraints here though.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -