SharpAspect 1.1.3

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package SharpAspect -Version 1.1.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package SharpAspect --version 1.1.3
<PackageReference Include="SharpAspect" Version="1.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpAspect --version 1.1.3
#r "nuget: SharpAspect, 1.1.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install SharpAspect as a Cake Addin
#addin nuget:?package=SharpAspect&version=1.1.3

// Install SharpAspect as a Cake Tool
#tool nuget:?package=SharpAspect&version=1.1.3

Getting Started

<a href="https://www.nuget.org/packages/SharpAspect/"> <img alt="Nuget (with prereleases)" src="https://img.shields.io/nuget/vpre/SharpAspect?label=SharpAspect%20%7C%20NuGet">

</a>

<a href="https://www.nuget.org/packages/SharpAspect/"> <img src="https://img.shields.io/nuget/dt/SharpAspect"> </a>

dotnet add package SharpAspect

Defining & mapping your Interceptors

All attributes must derive from MethodInterceptionAttribute class.

public class LogAttribute: MethodInterceptorAttribute
{

}

public class CacheAttribute: MethodInterceptorAttribute
{

}

<br>

All interceptors also must implement the IMethodInterceptor interface and should be marked with [Interceptor(typeof(TAttribute))].

[Interceptor(typeof(LogAttribute))]
public class LogInterceptor : IMethodInterceptor
{
    public void AfterInvoke(IInvocation invocation)
    {
        // throw new System.NotImplementedException();
    }

    public void BeforeInvoke(IInvocation invocation)
    {
        System.Console.WriteLine($"[Logging] {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}");
    }

    public void OnError(IInvocation invocation, System.Exception e)
    {
        throw new System.NotImplementedException();
        // System.Console.WriteLine(e.Message);
    }
}

[Interceptor(typeof(CacheAttribute))]
public class CacheInterceptor : IMethodInterceptor
{
    public void AfterInvoke(IInvocation invocation)
    {
        // throw new System.NotImplementedException();
    }

    public void BeforeInvoke(IInvocation invocation)
    {
        System.Console.WriteLine($"[Caching] {invocation.Method.DeclaringType.FullName}.{invocation.Method.Name}");
    }

    public void OnError(IInvocation invocation, System.Exception e)
    {
        throw new System.NotImplementedException();
    }
}

Registering your services

private static IServiceProvider ConfigureServices()
{
    return new ServiceCollection()
        .EnableDynamicProxy()

        // Transient service
        .AddTransientProxy<IRocket, Rocket>()

        .BuildServiceProvider();
}
public interface IRocket
{
    void Launch();
    string SetRoute(string route);
}

public class Rocket: IRocket
{
    [Log]
    public void Launch()
    {
        System.Console.WriteLine("Launching rocket in 3...2.....1 🚀");
    }

    [Log]
    [Cache]
    public string SetRoute(string route)
    {
        System.Console.WriteLine($"Route: {route}");
        return route;
    }
}
static void Main(string[] args)
{
    var services = ConfigureServices();

    var rocket = services.GetRequiredService<IRocket>();

    rocket.SetRoute("Moon");
    rocket.Launch();
}

Sample Output

[Logging] SharpAspect.Sample.IRocket.SetRoute
[Caching] SharpAspect.Sample.IRocket.SetRoute
Route: Moon

[Logging] SharpAspect.Sample.IRocket.Launch
Launching rocket in 3...2.....1 🚀
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.2 255 9/12/2021
4.1.1 157 5/5/2021
4.1.0 243 10/6/2020
4.0.0 237 10/6/2020
3.0.0 245 10/3/2020
2.2.4 216 10/3/2020
2.2.3 229 10/2/2020
2.2.3-beta 194 10/2/2020
1.2.3 258 10/2/2020
1.1.3 215 10/1/2020
0.1.3 221 10/1/2020