例如,考虑以下方法:
public static Attribute GetCustomAttribute(this ParameterInfo element, Type attributeType);
在 System.Reflection.CustomAttributeExtensions 中定义
这样定义不是更有意义吗:
public static T GetCustomAttribute<T>(this ParameterInfo element, T attributeType) where T : Attribute;
并保存类型转换?
请您参考如下方法:
检索自定义属性的非泛型方法来自未实现泛型的旧 .NET 时代。
对于当前和 future 的编码,您可以利用 CustomAttributeExtensions.GetCustomAttributes<T> - 如果您使用 .NET 4.5 及更高版本进行编码 -。
可悲的是 - 或者可能实际上 - 软件有一个连续的演变。我的意思是,曾经有一段时间我们没有泛型(.NET 1.0 和 1.1),并且有很多代码库是从早期的 .NET 版本继承的,而且由于框架团队的优先级,似乎并不是每个方法都适用使用已经实现的通用参数会更好。
关于继承的代码
@BenRobinson 在下面的一些评论中说:
The point I was making was that these extension methods were added in .net 4.5 (all of them not just the generic ones) and extension methods were added after generics so non generic extension methods of any kind have nothing to do with backwards compatibility with .net 1.0/1.1.
我添加这个是为了避免混淆:不理解继承代码,因为微软不会因为与第三方代码的向后兼容性而改变非通用代码库。
实际上,我要指出的是当前的 .NET 版本本身 有很多代码是从早期的 .NET 版本继承的,如果 Microsoft 的意图是保持与第三方代码的向后兼容性或不。
我假设或猜测 .NET Framework 团队已经优先考虑新的基类库 (BCL) 和附属框架的添加,一些来自前泛型时代的成员仍然原样,因为变化是不值得付出努力,或者我们可以讨论这是否值得付出努力,他们确实犯了设计决策错误,但 StackOverflow 不是讨论区,对吗?
