Cecil - 获取已定义的属性属性

时间:2009-08-07 05:10:37

标签: c# .net vb.net reflection mono.cecil

我正在使用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.为什么会这样?

由于

1 个答案:

答案 0 :(得分:2)

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

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

刚编译并检查过它。