Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. public static class StringExtensions
  2. {
  3. public static string Reverse(this string value)
  4. {
  5. char[] cArray = value.ToCharArray();
  6. Array.Reverse(cArray);
  7. return new string(cArray);
  8. }
  9. }
  10.  
  11. using System;
  12. using System.Runtime.CompilerServices;
  13. using System.Reflection;
  14. using System.Linq;
  15. using System.Collections.Generic;
  16.  
  17. public static class FirstExtensions
  18. {
  19. public static void Foo(this string x) {}
  20. public static void Bar(string x) {} // Not an ext. method
  21. public static void Baz(this int x) {} // Not on string
  22. }
  23.  
  24. public static class SecondExtensions
  25. {
  26. public static void Quux(this string x) {}
  27. }
  28.  
  29. public class Test
  30. {
  31. static void Main()
  32. {
  33. Assembly thisAssembly = typeof(Test).Assembly;
  34. foreach (MethodInfo method in GetExtensionMethods(thisAssembly,
  35. typeof(string)))
  36. {
  37. Console.WriteLine(method);
  38. }
  39. }
  40.  
  41. static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly,
  42. Type extendedType)
  43. {
  44. var query = from type in assembly.GetTypes()
  45. where type.IsSealed && !type.IsGenericType && !type.IsNested
  46. from method in type.GetMethods(BindingFlags.Static
  47. | BindingFlags.Public | BindingFlags.NonPublic)
  48. where method.IsDefined(typeof(ExtensionAttribute), false)
  49. where method.GetParameters()[0].ParameterType == extendedType
  50. select method;
  51. return query;
  52. }
  53. }
  54.  
  55. using System;
  56. using System.Collections.Generic;
  57. using System.Linq;
  58. using System.Reflection;
  59. using System.Runtime.CompilerServices;
  60.  
  61. namespace System
  62. {
  63. public static class TypeExtension
  64. {
  65. /// <summary>
  66. /// This Methode extends the System.Type-type to get all extended methods. It searches hereby in all assemblies which are known by the current AppDomain.
  67. /// </summary>
  68. /// <remarks>
  69. /// Insired by Jon Skeet from his answer on http://stackoverflow.com/questions/299515/c-sharp-reflection-to-identify-extension-methods
  70. /// </remarks>
  71. /// <returns>returns MethodInfo[] with the extended Method</returns>
  72.  
  73. public static MethodInfo[] GetExtensionMethods(this Type t)
  74. {
  75. List<Type> AssTypes = new List<Type>();
  76.  
  77. foreach (Assembly item in AppDomain.CurrentDomain.GetAssemblies())
  78. {
  79. AssTypes.AddRange(item.GetTypes());
  80. }
  81.  
  82. var query = from type in AssTypes
  83. where type.IsSealed && !type.IsGenericType && !type.IsNested
  84. from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
  85. where method.IsDefined(typeof(ExtensionAttribute), false)
  86. where method.GetParameters()[0].ParameterType == t
  87. select method;
  88. return query.ToArray<MethodInfo>();
  89. }
  90.  
  91. /// <summary>
  92. /// Extends the System.Type-type to search for a given extended MethodeName.
  93. /// </summary>
  94. /// <param name="MethodeName">Name of the Methode</param>
  95. /// <returns>the found Methode or null</returns>
  96. public static MethodInfo GetExtensionMethod(this Type t, string MethodeName)
  97. {
  98. var mi = from methode in t.GetExtensionMethods()
  99. where methode.Name == MethodeName
  100. select methode;
  101. if (mi.Count<MethodInfo>() <= 0)
  102. return null;
  103. else
  104. return mi.First<MethodInfo>();
  105. }
  106. }
  107. }
  108.  
  109. Type t = typeof(Type);
  110. MethodInfo[] extendedMethods = t.GetExtensionMethods();
  111. MethodInfo extendedMethodInfo = t.GetExtensionMethod("GetExtensionMethods");
  112.  
  113. public static IEnumerable<KeyValuePair<Type, MethodInfo>> GetExtensionMethodsDefinedInType(this Type t)
  114. {
  115. if (!t.IsSealed || t.IsGenericType || t.IsNested)
  116. return Enumerable.Empty<KeyValuePair<Type, MethodInfo>>();
  117.  
  118. var methods = t.GetMethods(BindingFlags.Public | BindingFlags.Static)
  119. .Where(m => m.IsDefined(typeof(ExtensionAttribute), false));
  120.  
  121. List<KeyValuePair<Type, MethodInfo>> pairs = new List<KeyValuePair<Type, MethodInfo>>();
  122. foreach (var m in methods)
  123. {
  124. var parameters = m.GetParameters();
  125. if (parameters.Length > 0)
  126. {
  127. if (parameters[0].ParameterType.IsGenericParameter)
  128. {
  129. if (m.ContainsGenericParameters)
  130. {
  131. var genericParameters = m.GetGenericArguments();
  132. Type genericParam = genericParameters[parameters[0].ParameterType.GenericParameterPosition];
  133. foreach (var constraint in genericParam.GetGenericParameterConstraints())
  134. pairs.Add(new KeyValuePair<Type, MethodInfo>(parameters[0].ParameterType, m));
  135. }
  136. }
  137. else
  138. pairs.Add(new KeyValuePair<Type, MethodInfo>(parameters[0].ParameterType, m));
  139. }
  140. }
  141.  
  142. return pairs;
  143. }
  144.  
  145. public List<MethodInfo> GetExtensionMethodsOf(Type t)
  146. {
  147. List<MethodInfo> methods = new List<MethodInfo>();
  148. Type cur = t;
  149. while (cur != null)
  150. {
  151.  
  152. TypeInfo tInfo;
  153. if (typeInfo.TryGetValue(cur.GUID, out tInfo))
  154. methods.AddRange(tInfo.ExtensionMethods);
  155.  
  156.  
  157. foreach (var iface in cur.GetInterfaces())
  158. {
  159. if (typeInfo.TryGetValue(iface.GUID, out tInfo))
  160. methods.AddRange(tInfo.ExtensionMethods);
  161. }
  162.  
  163. cur = cur.BaseType;
  164. }
  165. return methods;
  166. }
  167.  
  168. private Dictionary<Guid, TypeInfo> typeInfo = new Dictionary<Guid, TypeInfo>();
  169.  
  170. public class TypeInfo
  171. {
  172. public TypeInfo()
  173. {
  174. ExtensionMethods = new List<MethodInfo>();
  175. }
  176.  
  177. public List<ConstructorInfo> Constructors { get; set; }
  178.  
  179. public List<FieldInfo> Fields { get; set; }
  180. public List<PropertyInfo> Properties { get; set; }
  181. public List<MethodInfo> Methods { get; set; }
  182.  
  183. public List<MethodInfo> ExtensionMethods { get; set; }
  184. }
  185.  
  186. string rev = myStr.Reverse();
  187.  
  188. string rev = StringExtensions.Reverse(myStr);
  189.  
  190. CustomerExtension.Foo(myCustomer);
  191.  
  192. void Main()
  193. {
  194. var test = new Test();
  195. var testWithMethod = new TestWithExtensionMethod();
  196. Tools.IsExtensionMethodCall(() => test.Method()).Dump();
  197. Tools.IsExtensionMethodCall(() => testWithMethod.Method()).Dump();
  198. }
  199.  
  200. public class Test
  201. {
  202. public void Method() { }
  203. }
  204.  
  205. public class TestWithExtensionMethod
  206. {
  207. }
  208.  
  209. public static class Extensions
  210. {
  211. public static void Method(this TestWithExtensionMethod test) { }
  212. }
  213.  
  214. public static class Tools
  215. {
  216. public static MethodInfo GetCalledMethodInfo(Expression<Action> expr)
  217. {
  218. var methodCall = expr.Body as MethodCallExpression;
  219. return methodCall.Method;
  220. }
  221.  
  222. public static bool IsExtensionMethodCall(Expression<Action> expr)
  223. {
  224. var methodInfo = GetCalledMethodInfo(expr);
  225. return methodInfo.IsStatic;
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement