Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mustaddon/dispatchproxyadvanced
Extended version of DispatchProxy with Class proxying
https://github.com/mustaddon/dispatchproxyadvanced
custom-state dispatch-proxy dispatchproxy dotnet proxy-class proxy-factory proxy-handler proxy-interface proxy-object proxy-objects
Last synced: 28 days ago
JSON representation
Extended version of DispatchProxy with Class proxying
- Host: GitHub
- URL: https://github.com/mustaddon/dispatchproxyadvanced
- Owner: mustaddon
- License: mit
- Created: 2022-11-05T15:09:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-16T09:01:58.000Z (2 months ago)
- Last Synced: 2024-12-17T01:52:42.129Z (about 1 month ago)
- Topics: custom-state, dispatch-proxy, dispatchproxy, dotnet, proxy-class, proxy-factory, proxy-handler, proxy-interface, proxy-object, proxy-objects
- Language: C#
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DispatchProxyAdvanced [![NuGet version](https://badge.fury.io/nu/DispatchProxyAdvanced.svg?208)](http://badge.fury.io/nu/DispatchProxyAdvanced)
Extended version of DispatchProxy with Class proxying and custom states.### Example 1: Interface proxing
```C#
interface IExampleInterface
{
int MyProp { get; set; }
int MyMethod(int a, int b);
}
``````C#
using DispatchProxyAdvanced;var proxy1 = ProxyFactory.Create((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
```C#
class ExampleClass
{
public virtual int MyProp { get; set; }
public virtual int MyMethod(int a, int b) => a * b;
}
```
```C#
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
```C#
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](https://github.com/mustaddon/DispatchProxyAdvanced/tree/master/Examples/Program.cs)