OutWit.Common.Proxy.Generator 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package OutWit.Common.Proxy.Generator --version 1.0.0                
NuGet\Install-Package OutWit.Common.Proxy.Generator -Version 1.0.0                
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="OutWit.Common.Proxy.Generator" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OutWit.Common.Proxy.Generator --version 1.0.0                
#r "nuget: OutWit.Common.Proxy.Generator, 1.0.0"                
#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 OutWit.Common.Proxy.Generator as a Cake Addin
#addin nuget:?package=OutWit.Common.Proxy.Generator&version=1.0.0

// Install OutWit.Common.Proxy.Generator as a Cake Tool
#tool nuget:?package=OutWit.Common.Proxy.Generator&version=1.0.0                

OutWit.Common.Proxy.Generator

OutWit.Common.Proxy.Generator is a source generator that automatically creates proxy classes for interfaces marked with the ProxyTargetAttribute from the OutWit.Common.Proxy library.

Features

  • Automatically generates proxies for interfaces with events, methods, and properties.
  • Integrates seamlessly with custom IProxyInterceptor implementations.

Getting Started

Installation

Add OutWit.Common.Proxy.Generator to your project via NuGet:

dotnet add package OutWit.Common.Proxy.Generator

Usage

  1. Ensure your interface is marked with the ProxyTargetAttribute:

    using OutWit.Common.Proxy;
    
    [ProxyTarget]
    public interface IExampleService
    {
        string GetData(int id);
        event EventHandler DataChanged;
    }
    
  2. Implement IProxyInterceptor to handle invocations:

    public class ExampleInterceptor : IProxyInterceptor
    {
        public void Intercept(IProxyInvocation invocation)
        {
            Console.WriteLine($"Intercepted method: {invocation.MethodName}");
            if (invocation.MethodName == "GetData")
            {
                invocation.ReturnValue = $"Data for ID {invocation.Parameters[0]}";
            }
        }
    }
    
  3. Use the generated proxy in your code:

    var interceptor = new ExampleInterceptor();
    var proxy = new ExampleServiceProxy(interceptor);
    
    var result = proxy.GetData(42);
    Console.WriteLine(result); // Output: Data for ID 42
    

Support for Methods, Properties, and Events

Methods

When a method is called, a ProxyInvocation object is created and passed to the Intercept method. The return value of the method is taken from invocation.ReturnValue.

Properties

OutWit.Common.Proxy.Generator supports both getters and setters for properties. Each invocation is wrapped in a ProxyInvocation object, similar to methods.

Events

The generator handles subscription (add) and unsubscription (remove) for events, passing the relevant details to the Intercept method.


There are no supported framework assets in this 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
1.1.0 82 2/1/2025
1.0.0 71 1/11/2025