9

ここでは、AAtribute という名前のカスタム属性を作成しました。たとえば、1 つ以上のメソッドがこの属性を使用する B というクラスを作成しました。アセンブリ全体を調べたり、属性に対して定義されたすべてのメソッドを調べたりせずに、属性 (この場合は BMethod1) をその属性 (の 1 つ) として保持するメソッドの MethodInfo を取得することは可能ですか? そして、それらは他の AttributeTargets (パラメーター/タイプ/プロパティ/...) の類似の方法ですか? そのタイプの属性を使用するすべてのメソッドの配列は必要ありませんが、この属性オブジェクトを部分的に使用するメソッドだけが必要です。メソッドに追加の制約を設定するために使用したい (戻り値の型、パラメーター、名前、その他の属性の使用法など)。

[AttributeUsage(AttributeTargets.Method)]
public class AAtribute : Attribute {

    //some fields and properties

    public AAtribute () {//perhaps with some parameters
        //some operations
        MethodInfo mi;//acces to the MethodInfo with this Attribute
                      //as an Attribute (the question)
        //some operations with the MethodInfo
    }

    //some methods

}

public class B {

    //some fields, properties and constructors

    [A]
    public void BMethod1 () {
        //some operations
    }

    //other methods

}
4

3 に答える 3

2

答えはノーだと思います。または、少なくとも合理的な方法ではありません。属性のインスタンスは、MethodInfo を通じて属性を検索した場合にのみ構築されます。属性を持つメソッドを持つクラスをインスタンス化しても、属性はインスタンス化されません。属性インスタンスは、リフレクションを通じてそれらを見つけるためにあちこち探し始めたときにのみ作成されます。

于 2009-08-28T10:37:55.590 に答える
2

私があなたの質問を正しく理解していれば、属性 code 内で、属性が適用されるオブジェクト (この場合はメソッド) を取得する必要があります。
これを行う直接的な方法はないと確信しています。属性は、それがアタッチされているオブジェクトを認識していません。この関連付けは逆です。

私が提案できる最善の方法は、次のような回避策です。

using System;
using System.Reflection;

namespace test {

    [AttributeUsage(AttributeTargets.Method)]
    public class AAttribute : Attribute {
        public AAttribute(Type type,string method) {
            MethodInfo mi = type.GetMethod(method);
        }
    }

    public class B {
        [A(typeof(B),"BMethod1")]
        public void BMethod1() {
        }
    }
}


: 属性のコンストラクター内で MethodInfo にアクセスすることによって、何を達成したいですか? 目標を達成するための別の方法があるかもしれません...

編集

別の可能な解決策として、チェックを行う属性に静的メソッドを提供することもできますが、これには MethodInfos の反復処理が含まれます。

using System;
using System.Reflection;
namespace test {

    [AttributeUsage(AttributeTargets.Method)]
    public class AAttribute : Attribute {
        public static void CheckType<T>() {
            foreach (MethodInfo mi in typeof(T).GetMethods()) {
                AAttribute[] attributes = (AAttribute[])mi.GetCustomAttributes(typeof(AAttribute), false);
                if (0 != attributes.Length) {
                    // do your checks here
                }
            }
        }
    }

    public class B {
        [A]
        public void BMethod1() {
        }
        [A]
        public int BMethod2() {
            return 0;
        }
    }

    public static class Program {
        public static void Main() {
            AAttribute.CheckType<B>();
        }
    }
}
于 2009-08-28T10:19:06.657 に答える
0

メソッドに属性が適用されているかどうかを確認するには、すでに MethodInfo を持っています。

var type = obj.GetType();
foreach(var method in type.GetMethods())
{
    var attributes = method.GetCustomAttributes(typeof(AAtribute));
    if(attributes.Length > 0)
    {
        //this method has AAtribute applied at least once
    }
}

于 2009-08-28T10:02:05.897 に答える