Tag: 仿制药

如何在不知道封闭generics类型的情况下访问通用属性

我有一个通用类型如下 public class TestGeneric { public T Data { get; set; } public TestGeneric(T data) { this.Data = data; } } 如果我现在有一个对象(来自某个外部源),我知道它的类型是一些封闭的TestGeneric ,但我不知道TypeParameter T.现在我需要访问我的对象的数据。 问题是我无法转换对象,因为我不确切知道哪个封闭的TestGeneric。 我用 // thx to http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class private static bool IsSubclassOfRawGeneric(Type rawGeneric, Type subclass) { while (subclass != typeof(object)) { var cur = subclass.IsGenericType ? subclass.GetGenericTypeDefinition() : subclass; if (rawGeneric == cur) […]