使用PostSharp,无法使多播为WinForm控件单击处理程序工作

时间:2015-09-24 15:57:33

标签: c# winforms aop multicast postsharp

这是我的方面:

namespace AspectExtentions
{
    [MulticastAttributeUsage(MulticastTargets.Method | MulticastTargets.InstanceConstructor | MulticastTargets.StaticConstructor, TargetMemberAttributes = MulticastAttributes.Instance, AllowMultiple = true)]
    [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event | AttributeTargets.Interface, AllowMultiple = true)]
    [Serializable]
    public class LoggingAspect : OnMethodBoundaryAspect , IAspectProvider
    {
    .....  code not important
    }
}

用于:

namespace SynonymRedirector
{
    partial class Form1 : Form, IMainView
    {
        [LoggingAspect]
        private void ubCreateSynonyms_Click(object sender, EventArgs e)
        {
        .... code not important
        }
        ....
    }
}

这似乎按预期工作: 但是,我想删除此属性并使用MultiCast来定义切入点:

我定义了以下内容:(在SynonymRedirector.psproj中)

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
  <Multicast xmlns:my="clr-namespace:AspectExtentions;assembly:AspectExtentions">
    <my:LoggingAspect AttributeTargetTypes="SynonymRedirector.Form1.ubCreateSynonyms_Click" AttributeTargetMemberAttributes="Private" />
  </Multicast>
</Project>

我还定义了以下内容:(在AssemblyInfo.cs中)

[assembly: AspectExtentions.LoggingAspect(AttributeTargetMembers = "SynonymRedirector.Form1.ubSaveToFiles_Click", AttributeTargetElements = PostSharp.Extensibility.MulticastTargets.Method)]

但是,在没有注释掉的构建中,我收到以下错误:

Attribute 'AspectExtentions.LoggingAspect' is not valid on this declaration type. It is only valid on 'method' declarations.    C:\Projects\ProofOfConcept\SynonymRedirector\SynonymRedirector\Properties\AssemblyInfo.cs   18  12  SynonymRedirector

PostSharp似乎设置正确,因为在build上生成了以下内容:

Compile complete -- 0 errors, 10 warnings
  : message : PostSharp 4.1 [4.1.24.0, postsharp.srv.4.0-x86.exe, CLR 4.0.30319.379893, Release] complete -- 0 errors, 0 warnings, processed in 261 ms
  SynonymRedirector -> C:\Projects\ProofOfConcept\SynonymRedirector\SynonymRedirector\bin\Debug\SynonymRedirector.exe
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

我正在使用PostSharp的免费版本,但根据网站,Multicast应该可用。

我还注意到,当我将方面设置为click事件处理程序方法的属性并进行构建时,我得到很多f PostScript注释(警告)。但是,当我评论属性并进行构建时,我只获得简单的PostScript编译信息。在这种情况下,似乎没有运行PostScript代码生成。不知道为什么会这样。

所以,我的问题是:如何设置此属性,以便我可以使用Multicast在XML中设置切入点?我在SynonymRedirector.psproj或AssemblyInfo.cs文件中有什么不正确导致多播不起作用?

1 个答案:

答案 0 :(得分:0)

与组件的创建方式有关。更改为以下内容:

[assembly: AspectExtentions.LoggingAspect(AttributeTargetMembers= "ubCreateSynonyms_Click",
                                          AttributeTargetTypes = "SynonymRedirector.Form1")]