Skip to content
/ dynamxy Public

Library for .NET enabling the use of dynamic proxy objects.

License

Notifications You must be signed in to change notification settings

swzr/dynamxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynamxy

Library for .NET enabling the use of dynamic proxy objects.

Getting Started

Either fork, clone or download this repository. Or install the library via NuGet:

Install-Package SWZR.Dynamxy

How does it work

During instantiation of a proxy object, code is dynamically emitted using primarly IL to create a concrete implementation based on a given interface. This type functions as intermediary and forwards any method call to the predefined interceptor.

Usage

In order to use dynamic proxy objects first define an interface:

public interface ITestInterface
{
	object Echo(object obj);
}

Afterwards define an interceptor which handles all method calls:

using System.Reflection;
using SWZR.Dynamxy;

public class EchoInterceptor : IInterceptor
{
    public object InterceptMethod(object instance, MethodInfo info, object[] parameter)
    {
        return parameter.Length > 0 ? parameter[0] : null;
    }
}

The last step is to construct an instance of the interceptor and eventually the dynamic proxy itself:

var factory = new ProxyFactory<EchoInterceptor>();
var proxy = factory.Create<ITestInterface>();

// Will yield 'Hello World'.
var value = proxy.Echo("Hello World");

Use Cases

  • Authorization
  • Logging
  • REST
  • RPC
  • ...

Roadmap

  • Improve and extend unit tests
  • Allow intercepting methods of concrete types
  • Add an example

License

This project is licensed under the MIT License - see the LICENSE.md file for details

About

Library for .NET enabling the use of dynamic proxy objects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages