質問

私は自分の属性のプロパティを読み取るしようとするセシルを使用しています:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public sealed class TraceMethodAttribute : Attribute {
    public TraceMethodAttribute() {
        MethodStart = true;
        MethodReturn = true;
        MethodMessages = true;
    }

    public bool MethodStart { get; set; }
    public bool MethodReturn { get; set; }
    public bool MethodMessages { get; set; }
}

[TraceMethod(MethodMessages = false)]
static void Main(string[] args) {
}

...

if (attribute.Constructor.DeclaringType.FullName == typeof(TraceMethodAttribute).FullName) {         
  if ((bool)attribute.Fields["MethodMessages"] == true) {
        EditMethodStart(assembly, method);
  }

これは、私は、属性がメインに適用するたびにチェックするためにこのコードの最後のブロックを希望され、例えば、trueまたはfalseに設定MethodMessagesを持っています。それはなぜ?

attributes.Fields.Countとattributes.Properties.Countの両方が0に設定されているように、私が見てきたものから、それはそうです

おかげ

役に立ちましたか?

解決

インデクサにより、プロパティのコレクションへのアクセスを通じて正常に動作する必要があります。

if (attribute.Constructor.DeclaringType.FullName == typeof(TraceMethodAttribute).FullName) {         
  if ((bool)attribute.Properties["MethodMessages"] == true) {
        EditMethodStart(assembly, method);
  }

ただ、コンパイルして、それを確認します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top