c# - 使用 PostSharp,无法让 Multicast 为 WinForm 控件单击处理程序工作

标签 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 似乎设置正确,因为在构建时生成以下内容:

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 应该可用。

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

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

最佳答案

与程序集的创建方式有关。更改为以下内容:

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

关于c# - 使用 PostSharp,无法让 Multicast 为 WinForm 控件单击处理程序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32765866/

相关文章:

c# - 如何将 const int 从托管 C++ DLL 传递到 C# 以用作属性中的字段值

c# - 代码组织 : How to best group functions, 类等

c# - 捕获 C# 2.0 中作为类型参数给出的异常类型

java - Spring in Action 3 AOP引发问题的例子

c# - 按元素拆分列表

c# - 删除(x);它会处理元素 x 吗?

c# - 如何将 PDF 打印到默认打印机以外的其他打印机

c# - 如何将ListView内容保存到文本文件?

scala - 我可以在 Scala 中进行面向方面的编程吗?

java - spectj 切入点 - 拦截来自 javax.swing 内部的调用