2

我正在使用 Cecil 尝试读取我的属性属性:

[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);
  }

也就是说,我希望最后一段代码检查何时应用于 Main 的属性,例如,将 MethodMessages 设置为 true 或 false。从我所见,似乎 attributes.Fields.Count 和 attributes.Properties.Count 都设置为 0。为什么会这样?

谢谢

4

1 回答 1

2

通过索引器访问属性集合应该可以正常工作。

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

刚刚编译并检查了它。

于 2010-02-04T17:05:14.680 回答