セシル - 定義された属性のプロパティを取得します
-
12-09-2019 - |
質問
私は自分の属性のプロパティを読み取るしようとするセシルを使用しています:
[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);
}
ただ、コンパイルして、それを確認します。
所属していません StackOverflow