System.Reflection.DispatchProxy 4.8.0
About
Provides a DispatchProxy class to dynamically create proxy instances that implement a specified interface.
Key Features
Method invocations on a generated proxy instance are dispatched to a Invoke() method. Having a single invoke method allows centralized handling for scenarios such as logging, error handling and caching.
How to Use
Create the proxy class that derives from DispatchProxy, override Invoke() and call one of the static DispatchProxy.Create() methods to generate the proxy type.
The example below intercepts calls to the ICallMe interface and logs them.
class Program
{
static void Main(string[] args)
{
ICallMe proxy = LoggingDispatchProxy.Create<ICallMe>(new MyClass());
proxy.CallMe("Hello!");
}
}
public interface ICallMe
{
void CallMe(string name);
}
public class MyClass : ICallMe
{
public void CallMe(string message)
{
Console.WriteLine($"Inside the called method with input '{message}'");
}
}
public class LoggingDispatchProxy : DispatchProxy
{
private ICallMe _target;
protected override object Invoke(MethodInfo targetMethod, object[] args)
{
Console.WriteLine($"Calling method: '{targetMethod.Name}' with arguments: '{string.Join(", ", args)}'");
object result = targetMethod.Invoke(_target, args);
Console.WriteLine($"Called method: '{targetMethod.Name}'.");
return result;
}
public static T Create<T>(T target) where T : class
{
LoggingDispatchProxy proxy = DispatchProxy.Create<T, LoggingDispatchProxy>() as LoggingDispatchProxy;
proxy._target = (ICallMe)target;
return proxy as T;
}
}
Main Types
The main types provided by this library are:
- System.Reflection.DispatchProxy
Additional Documentation
- API reference can be found in: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.dispatchproxy
License
System.Reflection.DispatchProxy is released as open source under the MIT license.
Showing the top 20 packages that depend on System.Reflection.DispatchProxy.
| Packages | Downloads |
|---|---|
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
3f4f8eebd842c5f7f8b2941828c75384c2e2f1ab
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
209f8aa25c26bad137112523118aea6fe862b6c7
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
dcedc87d22ffdc1b86b6a1217dec61c4802ab517
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
2d61d0b043915bc948ebf98836fefe9ba942be11
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
4c728dfede639432bd306b4de3f5b49585f3de18
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
d43032d4d97ce28daf2347ed735ebceca2a1f260
When using NuGet 3.x this package requires at least version 3.4.
|
8 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
When using NuGet 3.x this package requires at least version 3.4.
|
7 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
124038c13eafa08e4bd8c2bfd04bb0633f958bc8
When using NuGet 3.x this package requires at least version 3.4.
|
7 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
85255dde3ecd86987236868a0415acd3265258d2
When using NuGet 3.x this package requires at least version 3.4.
|
7 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
caa7b7e2bad98e56a687fb5cbaf60825500800f7
When using NuGet 3.x this package requires at least version 3.4.
|
7 |
|
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
a9190d4a75f4a982ae4b4fa8d1a24526566c69df
When using NuGet 3.x this package requires at least version 3.4.
|
7 |
.NET Framework 4.6.2
- No dependencies.
.NET Core 3.1
- No dependencies.
.NET Standard 2.0
- System.Reflection.Emit (>= 4.7.0)
.NET Standard 2.1
- No dependencies.