Postsharp: my own aspect in shared library can't be used without additionally referencing postsharp - Why?

148 views Asked by At

As the title suggests, i have a problem reusing a custom aspect. I've created a very simple aspect (netstandard2.0) in a project called Postsharp.Why (Referencing nuget PostSharp 6.7.9-rc)

namespace Postsharp.Why
{
using System.Threading.Tasks;

using PostSharp.Aspects;
using PostSharp.Serialization;

[PSerializable]
public class ReasonAttribute : MethodInterceptionAspect
{
 
    private string _reason;

    public ReasonAttribute(string reason = "i fail to see")
    {
        this._reason = reason;
    }

    public sealed override void OnInvoke(MethodInterceptionArgs args)
    {
        args.ReturnValue = _reason;
    }

    public sealed override async Task OnInvokeAsync(MethodInterceptionArgs args)
    {
        args.ReturnValue = _reason;
    }
}
} 

Additionally i have created two further tests (xunit) projects

  • Postsharp.Question.Test.Fails default project setup + projectreference to Project.Why and
  • Postsharp.Question.Test.Works additionally references same Postsharp version.

Code:

namespace Postsharp.Question.Test.Works
{
using Postsharp.Why;

public class Test_PostSharp_Aspects
{
    [Reason("!")]
    private string foooooo()
    {
        return "?";
    }

    [Fact]
    public void Test_ShouldReturn_Exclamation()
    {
        Assert.Equal("!", foooooo());
    }
}
}

Note that all projects compile just fine, there is no error. As the name suggests, in the Fail-project the aspect does not work, and i would like to know how i can make it run (without adding the reference), or how i could point out that the used aspect won't run to anyone using that library. If this is not possible, i would like to know how i can enforce that anyone using the Postsharp.Why project can be enforced to install that package?

Thanks in advance!

1

There are 1 answers

0
Antonín Procházka On BEST ANSWER

Including PostSharp package reference in the library project (PostSharp.Why in this case) the following way makes the referencing projects being built using PostSharp:

<PackageReference Include="PostSharp" Version="6.7.9-rc" PrivateAssets="none" />

Packing your library into a NuGet package having the PostSharp package as a dependency would work as well, but unless you're going to publish your library, it is an overkill, as mentioned.