C# 当类型可能是泛型时,如何检测该类型的所有扩展方法?
我已经复习过了,还有其他的 我正在尝试为System.Type创建一个扩展方法,并为System.Reflection.MethodBase创建一个伴奏,以查找给定类型的所有扩展方法 我有以下资料:C# 当类型可能是泛型时,如何检测该类型的所有扩展方法?,c#,generics,reflection,extension-methods,C#,Generics,Reflection,Extension Methods,我已经复习过了,还有其他的 我正在尝试为System.Type创建一个扩展方法,并为System.Reflection.MethodBase创建一个伴奏,以查找给定类型的所有扩展方法 我有以下资料: public static class MethodBaseExtensions { public static bool IsExtensionMethodForType(this MethodBase method, Type type) { if (1 >
public static class MethodBaseExtensions
{
public static bool IsExtensionMethodForType(this MethodBase method, Type type)
{
if (1 > method.GetParameters().Count() || !method.IsDefined(typeof(ExtensionAttribute), false))
{
return false;
}
var extendedType = method.GetParameters().First().ParameterType;
if (!method.GetGenericArguments().Any(arg => arg.Equals(extendedType)))
{
if (extendedType.IsGenericType)
{
return extendedType.IsGenericTypeAssignableFrom(type);
}
return extendedType.IsAssignableFrom(type);
}
foreach (var constraint in extendedType.GetGenericParameterConstraints())
{
if (constraint.IsGenericType && !constraint.IsGenericTypeAssignableFrom(type))
{
return false;
}
if (!constraint.IsGenericType && !constraint.IsAssignableFrom(type))
{
return false;
}
}
return true;
}
}
由以下人员使用:
public static class TypeExtensions
{
public static MethodInfo GetExtensionMethod(this Type type, string name)
{
return type.GetExtensionMethods().Single(m => m.Name.Equals(name, StringComparison.Ordinal));
}
public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type)
{
return type.GetExtensionMethods(type.Assembly);
}
public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, Assembly assembly)
{
return type.GetExtensionMethods(new Assembly[] { assembly });
}
public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, IEnumerable<Assembly> assemblies)
{
const BindingFlags BINDING_FLAGS = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
return assemblies
.SelectMany(assembly => assembly.GetTypes().Where(t => t.IsExtensionMethodClass())
.SelectMany(t => t.GetMethods(BINDING_FLAGS))
.Where(m => m.IsExtensionMethodForType(type)));
}
public static bool IsGenericTypeAssignableFrom(this Type type, Type assignedType)
{
if (type.IsAssignableFrom(assignedType))
{
return true;
}
if (assignedType.IsGenericType &&
assignedType.GetGenericTypeDefinition().IsAssignableFrom(type))
{
return true;
}
foreach (var interfaceType in assignedType.GetInterfaces())
{
if (interfaceType.IsGenericType &&
interfaceType.GetGenericTypeDefinition().IsAssignableFrom(type))
{
return true;
}
}
if (default(Type) == assignedType.BaseType)
{
return false;
}
return type.IsGenericTypeAssignableFrom(assignedType.BaseType);
}
public static bool IsExtensionMethodClass(this Type type)
{
return type.IsSealed && !type.IsGenericType && !type.IsNested && type.IsDefined(typeof(ExtensionAttribute), false);
}
}
除非我调整MethodBaseExtensions.IsExtensionMethodForType
以获得这两种类型的通用类型定义,这最终会生成typeof(IFoo)。GetExtensionMethods()
也返回Test1
调试时,我发现IFoo
和IFoo
都变成了IFoo`1
,但它们显然是不同类型的IFoo`1
在这一点上,我正在追根究底,寻找解决方案
短暂性脑缺血发作
public static class IFooExtensions
{
public static void Test1(this IFoo<int> foo, int i)
{ }
public static void Test2<TFoo, T>(this TFoo foo, T t)
where TFoo : IFoo<T>
{ }
}