C# 如果使用自定义属性注入,则忽略本机PostSharp属性
考虑以下代码:C# 如果使用自定义属性注入,则忽略本机PostSharp属性,c#,attributes,aop,postsharp,C#,Attributes,Aop,Postsharp,考虑以下代码: [AttributeUsage(validOn: AttributeTargets.Property)] public sealed class ExcludeAttribute : Attribute { } [PSerializable] public sealed class PsDependencyPropertyAttribute : TypeLevelAspect, IAspectProvider { public PsDependencyProperty
[AttributeUsage(validOn: AttributeTargets.Property)]
public sealed class ExcludeAttribute : Attribute
{
}
[PSerializable]
public sealed class PsDependencyPropertyAttribute : TypeLevelAspect, IAspectProvider
{
public PsDependencyPropertyAttribute()
{
}
public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
{
var targetType = (Type)targetElement;
//---
var introduceObfuscationAspect =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(constructor: typeof(ObfuscationAttribute).GetConstructor(types: Type.EmptyTypes)));
introduceObfuscationAspect.CustomAttribute.NamedArguments.Add(key: "Feature", value: "renaming");
introduceObfuscationAspect.CustomAttribute.NamedArguments.Add(key: "Exclude", value: true);
introduceObfuscationAspect.CustomAttribute.NamedArguments.Add(key: "StripAfterObfuscation", value: true);
// add a Obfuscation attribute to every relevant property
foreach (var property
in targetType.GetProperties(
bindingAttr: BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Instance)
.Where(
predicate: property =>
property.CanWrite &&
!property.IsDefined(attributeType: typeof(ExcludeAttribute), inherit: false)))
yield return new AspectInstance(targetElement: property, aspect: introduceObfuscationAspect);
//---
//! NATIVE PostSharp ATTRIBUTES DON'T GET PROCESSED IF THEY'RE INJECTED AT COMPILE TIME
var introduceDependencyPropertyAspect =
new CustomAttributeIntroductionAspect(
new ObjectConstruction(constructor: typeof(DependencyPropertyAttribute).GetConstructor(types: Type.EmptyTypes)));
// add a DependencyPropertyA attribute to every relevant property
foreach (var property
in targetType.GetProperties(
bindingAttr: BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)
.Where(
predicate: property =>
property.CanWrite &&
!property.IsDefined(attributeType: typeof(ExcludeAttribute), inherit: false)))
yield return new AspectInstance(targetElement: property, aspect: introduceDependencyPropertyAspect);
}
}
[AttributeUsage(validOn:AttributeTargets.Property)]
公共密封类ExcludeAttribute:属性
{
}
[可序列化]
公共密封类PsDependencyPropertyAttribute:TypeLevelAspect,IAspectProvider
{
公共PsDependencyPropertyAttribute()
{
}
公共IEnumerable ProvideSpects(对象targetElement)
{
var targetType=(Type)targetElement;
//---
var简介=
新CustomAttributeIntroductionSpect(
新的ObjectConstruction(构造函数:typeof(ObfuscationAttribute).GetConstructor(类型:Type.EmptyTypes));
简介BusactionAspect.CustomAttribute.NamedArguments.Add(键:“功能”,值:“重命名”);
简介BusactionAspect.CustomAttribute.NamedArguments.Add(键:“排除”,值:true);
IntroductionBFUSCAtionAspect.CustomAttribute.NamedArguments.Add(键:“StripAfterObfuscation”,值:true);
//向每个相关属性添加模糊属性
foreach(var)属性
在targetType.GetProperties中(
bindingAttr:BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Instance)
.在哪里(
谓词:属性=>
property.CanWrite&&
!property.IsDefined(attributeType:typeof(ExcludeAttribute),inherit:false)))
收益返回新AspectInstance(targetElement:property,aspect:IntroductionBFusactionAspect);
//---
//!如果在编译时注入本机PostSharp属性,则不会对其进行处理
引入的var DependencyPropertySpect=
新CustomAttributeIntroductionSpect(
新的ObjectConstruction(构造函数:typeof(DependencyPropertyAttribute).GetConstructor(类型:Type.EmptyTypes));
//向每个相关属性添加DependencyPropertyA属性
foreach(var)属性
在targetType.GetProperties中(
bindingAttr:BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance)
.在哪里(
谓词:属性=>
property.CanWrite&&
!property.IsDefined(attributeType:typeof(ExcludeAttribute),inherit:false)))
收益返回新AspectInstance(targetElement:property,aspect:IntroductedependencyPropertySpect);
}
}
使用此自定义属性确实会注入两个属性
[DependencyProperty,Obfuscation(Feature=“renaming”,Exclude=true,StripAfterObfuscation=true)]
,唯一的问题是DependencyProperty
是一个PostSharp属性,不会被处理,只是被注入。这是合理的,因为定制属性告诉PS只需注入这两个属性,但是,当使用自定义属性注入PSdependencProperty时,有没有办法处理它?您可以直接提供dependencPropertyAttribute
作为目标属性的一个方面,而不是通过CustomAttributeIntroductionSpect
。例如:
yield return new AspectInstance(targetElement: property, aspect: new DependencyPropertyAttribute());
这就是作为属性引入时不处理DependencyPropertyAttribute
的原因:
PostSharp管道分几个阶段处理装配。首先执行属性处理,然后执行aspect Weaver。如果任何方面在此阶段发出新的自定义属性,则PostSharp将不再处理该属性,因为属性处理阶段已经完成