我有即時施加到一類的屬性以下代碼和屬性的類內:爲什麼ProvideAspects方法在實例化時不將屬性添加到IL?
public class SerialiseAttribute : Attribute, IAspectProvider, IValidableAnnotation {
public string ApplyToProperty { get; set; }
public string Name { get; set; }
public bool Ignore { get; set; }
bool IValidableAnnotation.CompileTimeValidate(object target) { return true; }
IEnumerable<AspectInstance> IAspectProvider.ProvideAspects(object targetElement) {
var type = targetElement as Type;
if (type != null && !FastSerialisationCacheAttribute.AppliedTo.Contains(type)) {
FastSerialisationCacheAttribute.AppliedTo.Add(type);
yield return new AspectInstance(type, new FastSerialisationCacheAttribute());
}
}
}
這intialises的FastSerialisationCacheAttribute並執行CompileTimeInitialize成功(其從TypeLevelAspect方面導出)。但是,當我檢查生成的IL時,在提供的類型上沒有FastSerialisationCacheAttribute,也不能在運行時找到使用反射的類型。
如果我切換出ProviderAspects功能與此代碼:
IEnumerable<AspectInstance> IAspectProvider.ProvideAspects(object targetElement) {
var type = targetElement as Type;
if (type != null && !FastSerialisationCacheAttribute.AppliedTo.Contains(type)) {
FastSerialisationCacheAttribute.AppliedTo.Add(type);
var constructor = typeof(FastSerialisationCacheAttribute).GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null);
var objectConstruction = new ObjectConstruction(constructor);
var introduceCacheAspect = new CustomAttributeIntroductionAspect(objectConstruction);
yield return new AspectInstance(type, introduceCacheAspect);
}
}
然後,它增加了屬性IL,但這並不初始化屬性(執行CompileTimeInitialize)。