基本クラスのメソッドからクラスのカスタム属性を取得できる必要があります。現在、次の実装を使用して、基本クラスの保護された静的メソッドを介してそれを行っています(クラスには、同じ属性の複数のインスタンスを適用できます)。
//Defined in a 'Base' class
protected static CustomAttribute GetCustomAttribute(int n)
{
return new StackFrame(1, false) //get the previous frame in the stack
//and thus the previous method.
.GetMethod()
.DeclaringType
.GetCustomAttributes(typeof(CustomAttribute), false)
.Select(o => (CustomAttribute)o).ToList()[n];
}
したがって、派生クラスから呼び出します。
[CustomAttribute]
[CustomAttribute]
[CustomAttribute]
class Derived: Base
{
static void Main(string[] args)
{
var attribute = GetCustomAttribute(2);
}
}
理想的には、コンストラクターからこれを呼び出して結果をキャッシュすることができます。
ありがとう。
PS
GetCustomAttributes が字句順序に関してそれらを返すことが保証されていないことを認識しています。