Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/robertturner/StaticProxyGenerator


https://github.com/robertturner/StaticProxyGenerator

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        

# StaticProxyGenerator
Interface proxy generator.
At compile time creates class that implements target interface. Instantiation of the generated class accepts an InterceptorHandler which is called for all method calls.

## Example

```
public interface IMyIfce
{
Task AsyncMethod(string arg1);
Task MethodWithNoReturn(int arg1, string arg2);
void SyncMethodNoArgs();
}
```
Create proxy of interface:
```
object interceptionHandler(object instance, MethodInfo method, object[] args, Type[] genericArguments)
{
switch (method.Name)
{
case nameof(IMyIfce.AsyncMethod):
return Task.FromResult((string)args[0]);
...
}
}

// Get instance of proxy
IMyIfce ifceProxy = ProxyGeneratorHelpers.InstantiateProxy(interceptionHandler);
```
Super happy place:
```
string result = await ifceProxy.AsyncMethod("an arg");
...
await ifceProxy.MethodWithNoReturn(4, "arg2");
...
ifceProxy.SyncMethodNoArgs();
```