Jep*_*sen 20 .net c# reflection custom-attributes base-class-library
为了避免在搜索已知类型的属性时使用过时的非泛型语法,通常会使用System.Reflection.CustomAttributeExtensions
类中的扩展方法(从.NET 4.5开始).
但是,如果在重写方法的返回参数(或重写的属性/索引器的访问者)上搜索属性,则这似乎会失败.
我正在使用.NET 4.6.1体验这一点.
简单复制(完整):
using System;
using System.Reflection;
namespace ReflectionTrouble
{
class B
{
//[return: MyMark("In base class")] // uncommenting does not help
public virtual int M() => 0;
}
class C : B
{
[return: MyMark("In inheriting class")] // commenting away attribute does not help
public override int M() => -1;
}
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] // commenting away AttributeUsage does not help
sealed class MyMarkAttribute : Attribute
{
public string Descr { get; }
public MyMarkAttribute(string descr)
{
Descr = descr;
}
public override string ToString() => $"MyMark({Descr})";
}
static class Program
{
static void Main()
{
var derivedReturnVal = typeof(C).GetMethod("M").ReturnParameter;
// usual new generic syntax (extension method in System.Refelction namespace):
var attr = derivedReturnVal.GetCustomAttribute<MyMarkAttribute>(); // BLOWS UP HERE, System.IndexOutOfRangeException: Index was outside the bounds of the array.
Console.WriteLine(attr);
// old non-generic syntax without extension method works:
var attr2 = ((MyMarkAttribute[])(derivedReturnVal.GetCustomAttributes(typeof(MyMarkAttribute), false)))[0]; // OK
Console.WriteLine(attr2);
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码可能看起来"太长而无法读取",但它实际上只是一个重写方法,其返回参数具有属性,并且明显尝试检索该属性实例.
堆栈跟踪:
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Attribute.GetParentDefinition(ParameterInfo param) at System.Attribute.InternalParamGetCustomAttributes(ParameterInfo param, Type type, Boolean inherit) at System.Attribute.GetCustomAttributes(ParameterInfo element, Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttribute(ParameterInfo element, Type attributeType, Boolean inherit) at System.Reflection.CustomAttributeExtensions.GetCustomAttribute[T](ParameterInfo element) at ReflectionTrouble.Program.Main() in c:\MyPath\Program.cs:line 38
我做了什么明显错误的事吗?
这是一个错误,如果是,它是众所周知的,它是一个旧的错误?
这确实看起来像一个bug.问题似乎与继承有关.这确实有效:
ReturnParameter.GetCustomAttribute<MyMark>(inherit: false)
Run Code Online (Sandbox Code Playgroud)
检索属性有两个不同的代码路径:( MemberInfo.GetCustomAttribute
较旧)和Attribute.GetCustomAttribute
(较新的和推荐的).还有通用扩展方法,它们使用后者,更新的方法.它们之间的区别确实是它们处理继承的方式..NET 1.0忽略了inherit
属性,事件和参数的参数.所以为了不破坏事物,Attribute
我们在.NET 2.0中引入了静态方法(连同这个bug).
看起来他们在上传继承树时忽略了返回值参数的特殊情况(参见此处).您可以在GitHub仓库或Connect错误中打开一个问题.