Advanced runtime-proxy generation for .NET with support for interfaces, classes, and per-proxy state.
DispatchProxyAdvanced extends the standard DispatchProxy pattern and makes it easier to create dynamic proxy objects for interfaces, abstract classes, and concrete classes while keeping custom state on each proxy instance.
- Proxy interfaces and classes at runtime
- Create proxies from virtual members without writing a custom implementation class
- Forward calls to a source instance for class-based scenarios
- Attach custom state to each generated proxy instance
- Useful for logging, instrumentation, validation, caching, and interception patterns
dotnet add package DispatchProxyAdvancedusing DispatchProxyAdvanced;
public interface IExampleInterface
{
int MyProp { get; set; }
int MyMethod(int a, int b);
}
var proxy = ProxyFactory.Create<IExampleInterface>((method, args) =>
{
Console.WriteLine($"Executing method: {method.Name}, args: {string.Join(", ", args)}");
return args.FirstOrDefault();
});
proxy.MyProp = 222;
proxy.MyMethod(20, 200);
// Console output:
// Executing method: set_MyProp, args: 222
// Executing method: MyMethod, args: 20, 200This is useful when you need to intercept interface calls without implementing a dedicated proxy class.
using DispatchProxyAdvanced;
public class ExampleClass
{
public virtual int MyProp { get; set; }
public virtual int MyMethod(int a, int b) => a * b;
}
var someInstance = new ExampleClass { MyProp = 111 };
var proxy = ProxyFactory.CreateSourced(someInstance, (source, method, args) =>
{
Console.WriteLine($"Executing method: {method.Name}, with args: {string.Join(", ", args)}");
return method.Invoke(source, args);
});
Console.WriteLine($"Property value: {proxy.MyProp}");
Console.WriteLine($"Method result: {proxy.MyMethod(10, 100)}");
// Console output:
// Executing method: get_MyProp, with args:
// Property value: 111
// Executing method: MyMethod, with args: 10, 100
// Method result: 1000The generated proxy delegates virtual calls to the original instance while allowing interception logic around them.
var proxy = ProxyFactory.CreateSourced(someInstance, (source, proxy, method, args) =>
{
// set your custom state to proxy instance
proxy.CustomProxyStateDefinition = "ExampleStateValue";
Console.WriteLine($"Executing method: {method.Name}");
return method.Invoke(source, args);
});
Console.WriteLine($"Start proxy state: '{((IProxy)proxy).CustomProxyStateDefinition}'");
proxy.MyMethod(10, 100);
Console.WriteLine($"Current proxy state: '{((IProxy)proxy).CustomProxyStateDefinition}'");
// Console output:
// Start proxy state: ''
// Executing method: MyMethod
// Current proxy state: 'ExampleStateValue'Each proxy instance can carry its own custom state through the IProxy interface.
This library is useful when you need interception or decoration without manually writing a proxy type. Common scenarios include:
- logging and diagnostics
- method tracing
- validation or policy checks
- introducing cross-cutting behavior in a lightweight way
- dynamic runtime wrappers for interfaces and classes
See the sample application in Examples/Program.cs.
This project is licensed under the MIT License.