The Roslyn Source Generator for the OutWit proxy library. Add this package to your project to automatically generate proxy implementations for your interfaces at compile-time.
$ dotnet add package OutWit.Common.Proxy.GeneratorOutWit.Common.Proxy.Generator is a source generator that automatically creates proxy classes for interfaces marked with the ProxyTargetAttribute from the OutWit.Common.Proxy library.
IProxyInterceptor implementations.Add OutWit.Common.Proxy.Generator to your project via NuGet:
dotnet add package OutWit.Common.Proxy.Generator
Ensure your interface is marked with the ProxyTargetAttribute:
using OutWit.Common.Proxy;
[ProxyTarget]
public interface IExampleService
{
string GetData(int id);
event EventHandler DataChanged;
}
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]}";
}
}
}
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
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.
OutWit.Common.Proxy.Generator supports both getters and setters for properties. Each invocation is wrapped in a ProxyInvocation object, similar to methods.
The generator handles subscription (add) and unsubscription (remove) for events, passing the relevant details to the Intercept method.
Licensed under the Apache License, Version 2.0. See LICENSE.
If you use OutWit.Common.Proxy.Generator in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.Proxy.Generator (https://ratner.io/)".
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
You may not: