我使用的塞西尔尝试读取我的属性属性:

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

这是,我想的代码这个最后块检查每当施加到主属性,例如,具有被设置为真或假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