新手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衍生屬性丟失。