注意,扩展方法本质上是静态方法,所以拿到MethodInfo时,应该这么调用 methodInfo.Invoke(null, new object[]{params})
1 static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType) 2 { 3 var query = from type in assembly.GetTypes() 4 where !type.IsGenericType && !type.IsNested 5 from method in type.GetMethods(BindingFlags.Static 6 | BindingFlags.Public | BindingFlags.NonPublic) 7 where method.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false) 8 where method.GetParameters()[0].ParameterType == extendedType 9 select method; 10 return query; 11 }
时间: 2024-10-10 09:50:54