Dbarone.Net.Proxy 1.0.1

dotnet add package Dbarone.Net.Proxy --version 1.0.1
                    
NuGet\Install-Package Dbarone.Net.Proxy -Version 1.0.1
                    
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="Dbarone.Net.Proxy" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dbarone.Net.Proxy" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Dbarone.Net.Proxy" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Dbarone.Net.Proxy --version 1.0.1
                    
#r "nuget: Dbarone.Net.Proxy, 1.0.1"
                    
#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.
#:package Dbarone.Net.Proxy@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Dbarone.Net.Proxy&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Dbarone.Net.Proxy&version=1.0.1
                    
Install as a Cake Tool

Dbarone.Net.Proxy

A .NET Proxy / Decorator generator using interceptors.

Although .NET Core does not include remoting functionality or the RealProxy class found in .NET Framework, it does include a DispatchProxy class in the System.Reflection namespace which can be used for basic aspect oriented programming (AOP) use cases like logging, caching, and security.

The main class in this library is the ProxyGenerator class. This class can be used to generate proxy objects that wrap the original object, and provide 'Before' and 'After' interception hooks denoted by the BoundaryType enum:

Interception Hook Invoked
Before Before the target method is called.
After After the method is called.

Interceptors

The target object is decorated using an interceptor method. The interceptor method must accept an InterceptorArgs parameter which has the following properties:

Parameter Type Description
BoundaryType BoundaryType Contains the interception hook (Before, After, Exception).
TargetMethod MethodInfo The target method being invoked.
Args object?[]? The parameters used for the target method invocation.
Result object? The result of the target method invocation.
Continue bool Can be set to false to stop further execution of the method. Set to false when handling exceptions to swallow the exception.
Exception Exception Any exception if thrown from the target method invocation.

Exceptions

If an exception is thrown in the target method invocation, the 'After' interception handler is guaranteed to be called, and the Exception object will be passed into the interception handler via the Exception property. If the Continue interception property is set to false, the exception will be swallowed. Otherwise, the original method will continue to throw the error after the interception handler completes.

Cancelling

In the Before interception handler, setting the Continue property to false will stop the target method being invoked. In the After interception handler, setting the Continue property to false when an exception is thrown in the target method will result in the exception being swallowed.

Sample Code

The following code snippet shows how the ProxyGenerator can be used:

namespace Dbarone.Net.Proxy.Tests;
using Dbarone.Net.Proxy;
using System.Reflection;

public interface IAnimal
{
    public string MakeSound();
    public void Fly();
    public string Name { get; set; }
}

public class Dog : IAnimal
{
    public string MakeSound() { return "woof"; }
    public string Name { get; set; }

    public void Fly()
    {
        throw new Exception("Dogs can't fly!");
    }

    public Dog(string name)
    {
        Name = name;
    }
}

/// <summary>
/// Simple interceptor class.
/// </summary>
public class DuckInterceptor<T>
{
    public void Interceptor(InterceptorArgs<T> interceptorArgs)
    {
        // Change MakeSound behaviour on all animals
        if (interceptorArgs.BoundaryType==BoundaryType.After && interceptorArgs.TargetMethod.Name=="MakeSound" ) {
            interceptorArgs.Result = "quack";
        }
    }
}

public class Program
{
    public void Main()
    {
        var dog = new Dog("Fido");
        var interceptor = new DuckInterceptor<IAnimal>();
        var generator = new ProxyGenerator<IAnimal>();
        generator.Interceptor = interceptor.Interceptor;
        var proxy = generator.Decorate(dog);
        proxy.MakeSound();          // "quack"
    }
}

Documentation

For full details of the library, please refer to the documentation.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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
1.0.1 613 7/2/2022
1.0.0 534 7/2/2022