SharpAspect 1.1.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpAspect --version 1.1.3
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.
<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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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 418 9/12/2021
4.1.1 287 5/5/2021
4.1.0 371 10/6/2020
4.0.0 362 10/6/2020
3.0.0 395 10/3/2020
2.2.4 357 10/3/2020
2.2.3 366 10/2/2020
2.2.3-beta 239 10/2/2020
1.2.3 399 10/2/2020
1.1.3 346 10/1/2020
0.1.3 358 10/1/2020