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
Provides a set of packages that can be used when building portable libraries on .NETCore based platforms.
|
89 |
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.
|
87 |
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
1a165a1588309bd8c34f77e1f926e6dbf6ae30d0
When using NuGet 3.x this package requires at least version 3.4.
|
105 |
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
811c3ce6c01229f0eaead96f3eb10112a9066b34
When using NuGet 3.x this package requires at least version 3.4.
|
89 |
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
b61e1596d6afb8168ebd9a46d560af4413c6d57f
When using NuGet 3.x this package requires at least version 3.4.
|
100 |
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
d6a56162401485f1c898ce7e78f95641954997b3
When using NuGet 3.x this package requires at least version 3.4.
|
88 |
Microsoft.NETCore.App
A set of .NET API's that are included in the default .NET Core application model.
eb9bc9205169e4c00d218d03167a564a7429921f
When using NuGet 3.x this package requires at least version 3.4.
|
90 |
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.
|
89 |
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.
|
94 |
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.
|
96 |
System.Private.ServiceModel
Internal implementation package not meant for direct consumption. Please do not reference directly. Provides implementation of System.ServiceModel packages.
133512ecc8d49b69010bdc737a2c85e9401842d2
When using NuGet 3.x this package requires at least version 3.4.
|
94 |
System.Private.ServiceModel
Internal implementation package not meant for direct consumption. Please do not reference directly. Provides implementation of System.ServiceModel packages.
32c5f3642810d5731e67683ba415757fa7ad1a6c
When using NuGet 3.x this package requires at least version 3.4.
|
95 |
System.Private.ServiceModel
Internal implementation package not meant for direct consumption. Please do not reference directly. Provides implementation of System.ServiceModel packages.
8ee6d65a6da34d4d37ce1d00d87febfeed0561a9
When using NuGet 3.x this package requires at least version 3.4.
|
108 |
System.Private.ServiceModel
Internal implementation package not meant for direct consumption. Please do not reference directly. Provides implementation of System.ServiceModel packages.
ef12c524d29edfd2f5db96cf1ac60b2372986790
When using NuGet 3.x this package requires at least version 3.4.
|
116 |
System.Private.ServiceModel
Package Description
|
91 |
System.Private.ServiceModel
Package Description
|
94 |
System.Private.ServiceModel
Package Description
|
96 |
System.Private.ServiceModel
Package Description
|
103 |
System.Private.ServiceModel
Package Description
|
110 |
System.Private.ServiceModel
Package Description
|
112 |
.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.