C# PostSharp LocationInterceptionSpect未应用于继承的属性

C# PostSharp LocationInterceptionSpect未应用于继承的属性,c#,inheritance,postsharp,aop,C#,Inheritance,Postsharp,Aop,我创建了一个继承LocationInterceptionSpect的属性。 出于演示目的,代码如下所示: [Serializable] public class RepeaterAttribute : LocationInterceptionAspect { public override bool CompileTimeValidate(PostSharp.Reflection.LocationInfo locationInfo) { var propertyI

我创建了一个继承LocationInterceptionSpect的属性。 出于演示目的,代码如下所示:

[Serializable]
public class RepeaterAttribute : LocationInterceptionAspect
{
    public override bool CompileTimeValidate(PostSharp.Reflection.LocationInfo locationInfo)
    {
        var propertyInfo = locationInfo.PropertyInfo;
        if (propertyInfo == null) return false;
        if (propertyInfo.PropertyType != typeof(String))
            return false;
        return base.CompileTimeValidate(locationInfo);
    }
    public override void OnSetValue(LocationInterceptionArgs args)
    {
        args.Value = ((String)args.Value) + ((String)args.Value);
        args.ProceedSetValue();
    }
}
我有一个名为External的库,其中有一个名为Parent的类

namespace External
{
    public class Parent
    {
        public String ParentProperty { get; set; }
    }
}
在控制台应用程序中,我有一个名为Child的类,它从父级继承。 控制台应用程序引用外部库

public class Child : External.Parent
{
    public String ChildProperty { get; set; }
}
在我的控制台应用程序中,我的代码是

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var child = new Child();
            child.ParentProperty = "A";
            Console.WriteLine("This should be 'AA' : '{0}'", child.ParentProperty);
            child.ChildProperty = "B";
            Console.WriteLine("This should be 'BB' : '{0}'", child.ChildProperty);
            Console.ReadKey();
        }
    }
}
在控制台应用程序的AssemblyInfo.cs中,我有:

[assembly: ConsoleApplication.Repeater(AttributeTargetTypes = "ConsoleApplication.Child")]

但是,当我运行Repeater属性时,该属性不会应用于从父类继承的“ParentProperty”。

PostSharp无法更改它正在转换的程序集中的不同程序集中的类。基属性在不同的程序集中声明。这是LocationInterceptionSpect的一个限制

您可以使用MethodInterception,它支持不同程序集中的拦截方法:

[Serializable]
public class SetterRepeaterAttribute : MethodInterceptionAspect
{
    public override void OnInvoke( MethodInterceptionArgs args )
    {
        args.Arguments[0] = ((String)args.Arguments[0]) + ((String)args.Arguments[0]);
        args.Proceed();
    }
}
并在程序集级别将其多播到基类的setter:

[assembly: ConsoleApplication2.SetterRepeater(
    AttributeTargetMembers = "set_ParentProperty", 
    AttributeTargetTypes = "External.Parent", 
    AttributeTargetAssemblies = "regex:.*")]

请注意,在这种情况下,拦截是在调用站点级别完成的,ParentProperty setter本身不会更改。来自原始程序集的调用将不会被拦截。

PostSharp无法更改它正在转换的程序集中的不同程序集中的类。基属性在不同的程序集中声明。这是LocationInterceptionSpect的一个限制

您可以使用MethodInterception,它支持不同程序集中的拦截方法:

[Serializable]
public class SetterRepeaterAttribute : MethodInterceptionAspect
{
    public override void OnInvoke( MethodInterceptionArgs args )
    {
        args.Arguments[0] = ((String)args.Arguments[0]) + ((String)args.Arguments[0]);
        args.Proceed();
    }
}
并在程序集级别将其多播到基类的setter:

[assembly: ConsoleApplication2.SetterRepeater(
    AttributeTargetMembers = "set_ParentProperty", 
    AttributeTargetTypes = "External.Parent", 
    AttributeTargetAssemblies = "regex:.*")]
请注意,在这种情况下,拦截是在调用站点级别完成的,ParentProperty setter本身不会更改。来自原始程序集的调用将不会被拦截