如果使用自定义属性注入,则将忽略本机PostSharp属性

时间:2018-08-16 16:04:54

标签: c# attributes aop postsharp

考虑以下代码:

 [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);
    }
}

使用此自定义属性确实确实注入了2个属性[DependencyProperty, Obfuscation(Feature = "renaming", Exclude = true, StripAfterObfuscation = true)],唯一的问题是DependencyProperty是PostSharp属性,只是注入而不会被处理。这是合理的,因为自定义属性告诉PS只是注入这两个属性,但是当使用自定义属性注入PS DependencyProperty时,有没有办法处理它?<​​/ p>

1 个答案:

答案 0 :(得分:1)

您可以直接将DependencyPropertyAttribute作为目标属性的一个方面提供,而不必通过CustomAttributeIntroductionAspect。例如:

yield return new AspectInstance(targetElement: property, aspect: new DependencyPropertyAttribute());

这就是为什么在引入DependencyPropertyAttribute作为属性时不进行处理的原因:

PostSharp管道分多个阶段处理装配。首先执行属性的处理,然后执行方面编织器。如果在此阶段中任何方面正在发出新的自定义属性,则由于属性处理阶段已经完成,PostSharp将不再处理该属性。