【发布时间】:2015-04-24 22:04:51
【问题描述】:
PostSharp 的新手。考虑以下代码:
using System;
using PostSharp.Aspects;
namespace PostSharp1 {
[AttributeUsage(AttributeTargets.Property)][Serializable]public class Field1Attribute : System.Attribute { }
[AttributeUsage(AttributeTargets.Property)][Serializable]public class Field2Attribute : LocationInterceptionAspect { }
public class Person {
[Field1][Field2]public string Name { get; set; }
}
class Program {
static void Main(string[] args) {
var Friend = new Person();
Friend.Name = "Fred Bloggs";
var Properties = Friend.GetType().GetProperties();
Console.WriteLine("Properties: " + Properties.Length);
var Count1 = 1;
foreach (var Property in Properties) {
var CustomAttributes = Property.GetCustomAttributes(false);
Console.WriteLine(" Property #" + Count1++ + ": " + Property.Name + ", # custom attributes = " + CustomAttributes.Length);
var Count2 = 1;
foreach (System.Attribute CustomAttribute in CustomAttributes) {
Console.WriteLine(" Attribute #" + Count2++ + ": " + CustomAttribute.ToString());
}
}
}
}
}
一个虚构的例子,它使用反射来列出一个小型 Person 类的属性的自定义属性。
程序列出 Field1Attribute(基于 System.Attribute),但 Field2Attribute 似乎已被删除,因为它没有列出。
只是想了解这里的机制以及为什么缺少 LocationInterceptionAspect 派生属性。
【问题讨论】:
标签: custom-attributes postsharp