C#反射积累
·
public interface IBase { }
public interface IDerived : IBase { }
public interface IOther { }
public class MyClass : IDerived, IOther { }
public static List<Type> GetDirectlyImplementedInterfaces(Type type)
{
// Get all interfaces implemented by the class
var allInterfaces = type.GetInterfaces();
// Create a set of all inherited interfaces
var inheritedInterfaces = new HashSet<Type>(allInterfaces.SelectMany(i => i.GetInterfaces()));
// Direct interfaces are those that are in allInterfaces but not in inheritedInterfaces
var directInterfaces = allInterfaces.Where(i => !inheritedInterfaces.Contains(i)).ToList();
return directInterfaces;
}
/
IDerived
IOther
/
--------------------------------------------
现代常用(type).GetTypeInfo().ImplementedInterfaces
因为他返回IEnumerable<TypeInfo>,可以linq。
上面那个需要AsEnumerable
泛型反射
Type genericDefinition = typeof(List<>);
Console.WriteLine(genericDefinition.FullName);
// 输出:System.Collections.Generic.List`1[T]
Console.WriteLine(genericDefinition.IsGenericTypeDefinition); 是不是泛型模版
// 输出:True(是泛型类型定义)
Console.WriteLine(genericDefinition.IsGenericType); // 输出:True
Console.WriteLine(genericDefinition.GetGenericArguments().Length);
// 输出:1(有1个类型参数占位符 T)
------------------------------------------------------------------
Type constructedType = typeof(List<int>);
Console.WriteLine(constructedType.FullName);
// 输出:System.Collections.Generic.List`1[System.Int32]
Console.WriteLine(constructedType.IsGenericType);
// 输出:True(是泛型类型)
Console.WriteLine(constructedType.IsGenericTypeDefinition);
// 输出:False(不是模板,是具体类型)
// 反向获取泛型模板(List<>)
Type originalDefinition = constructedType.GetGenericTypeDefinition();
Console.WriteLine(originalDefinition == typeof(List<>));
// 输出:True
-------------------------------------------------------------------
public void Test<T>()
{
// 这里的 List<T> 是“构造泛型类型”,但 T 未确定(可能是 int、string 等)
Type type = typeof(List<T>);
Console.WriteLine(type.FullName);
// 调用 Test<int>() 时输出:System.Collections.Generic.List`1[System.Int32]
// 调用 Test<string>() 时输出:System.Collections.Generic.List`1[System.String]
Console.WriteLine(type.IsGenericTypeDefinition);
// 输出:False(因为 T 是具体传入的类型,不是占位符)
}
// 调用示例
Test<int>(); // List<T> 对应 List<int> 的 Type
Test<string>(); // List<T> 对应 List<string> 的 Type
//如果不在泛型声明中直接写 List<T>,会报错
// 编译报错:找不到类型或命名空间名 'T'(是否缺少 using 指令或程序集引用?)
Type type = typeof(List<T>);
------------------------------------------------------------------------
// 假设需要动态创建 List<Video>(Video 是你的视频实体类)
Type videoType = typeof(Video);
Type listDefinition = typeof(List<>); // 泛型模板
Type listVideoType = listDefinition.MakeGenericType(videoType); // 构建 List<Video> 类型
// 动态实例化 List<Video>(等价于 new List<Video>())
IList<Video> videoList = (IList<Video>)Activator.CreateInstance(listVideoType);
-------------------------------------------------------------------------
// 获取 List<> 的泛型类型定义(模板)
Type listTemplate = typeof(List<>);
// 获取 List<int> 的构造泛型类型(实例化后的具体类型)
Type listTemplate_Int = typeof(List<int>);
// 获取泛型模板的类型参数占位符
Type[] typeArguments = listTemplate.GetGenericArguments();
// 获取构造泛型类型的具体类型参数
Type[] ints = listTemplate_Int.GetGenericArguments();
// 输出结果
Console.WriteLine(typeArguments[0]); // 输出:T
Console.WriteLine(ints[0]); // 输出:System.Int32
更多推荐
所有评论(0)