新手PostSharp时失踪。考虑以下代码:自定义属性使用基于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衍生属性丢失。