【发布时间】:2016-06-01 13:56:34
【问题描述】:
我有一个使用泛型的类型。让我们称之为FlowerDescriptor<T> 一些花是用数字来描述的,另一些是用字符串等来描述的。
所以FlowerDescriptor<int>;FlowerDescriptor<string>;等等
我想要一种机制(可能是扩展方法)来做两件事
- 看看有没有东西是
FlowerDescriptor和 - 查看描述符是什么。
即
-
FlowerDescriptor<string>.GetType().IsFlowerDescriptor == true string.GetType().IsFlowerDescriptor == false。
同样,我可能来自FlowerDescriptor<int>,即class NumberedFlower: FlowerDescriptor<int>
new NumberedFlower.GetType().IsFlowerDesriptor == true;
- 同上,但返回类型
FlowerDescriptor<string>.GetType().GetFlowerDescriptor() == typeof(string) FlowerDescriptor<int>.GetType().GetFlowerDescriptor() == typeof(int) new NumberedFlower.GetType().GetFlowerDescriptor() == typeof(int)
我玩过IsAssignableFrom 的变体,感觉这应该适用于typeof(FlowerDescriptor<>).IsAssignableFrom(typeof(FlowerDescriptor<string>))
但它不起作用。如果它添加泛型类型,它会这样做。
我目前正在探索GetInterfaces 以了解可用的接口。能真正理解我做错了什么也很棒..
【问题讨论】:
标签: c# .net generics inheritance