Skip to content

Extended version of DispatchProxy with Class proxying

License

Notifications You must be signed in to change notification settings

mustaddon/DispatchProxyAdvanced

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DispatchProxyAdvanced NuGet version

Extended version of DispatchProxy with Class proxying and custom states.

Example 1: Interface proxing

interface IExampleInterface
{
    int MyProp { get; set; }
    int MyMethod(int a, int b);
}
using DispatchProxyAdvanced;

var proxy1 = ProxyFactory.Create<IExampleInterface>((method, args) =>
{
    Console.WriteLine($"Executing method: {method.Name}, with args: {string.Join(", ", args)}");
    return args.FirstOrDefault();
});

proxy1.MyProp = 222;
proxy1.MyMethod(20, 200);

// Console output: 
// Executing method: set_MyProp, with args: 222
// Executing method: MyMethod, with args: 20, 200

Example 2: Class proxing with source instance

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 proxy3 = 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: {proxy3.MyProp}");
Console.WriteLine($"Method result: {proxy3.MyMethod(10, 100)}");

// Console output: 
// Executing method: get_MyProp, with args:
// Property value: 111
// Executing method: MyMethod, with args: 10, 100
// Method result: 1000

Example 3: Custom proxy instance state

var proxy4 = 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)proxy4).CustomProxyStateDefinition}'");

proxy4.MyMethod(10, 100);

Console.WriteLine($"Current proxy state: '{((IProxy)proxy4).CustomProxyStateDefinition}'");

// Console output: 
// Start proxy state: ''
// Executing method: MyMethod
// Current proxy state: 'ExampleStateValue'

Program.cs

Releases

No releases published

Packages

No packages published