ProxyDispatch 1.0.4
dotnet add package ProxyDispatch --version 1.0.4
NuGet\Install-Package ProxyDispatch -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ProxyDispatch" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ProxyDispatch --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ProxyDispatch, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ProxyDispatch as a Cake Addin
#addin nuget:?package=ProxyDispatch&version=1.0.4
// Install ProxyDispatch as a Cake Tool
#tool nuget:?package=ProxyDispatch&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
ProxyDispatch
基于 DispatchProxy 和 Roslyn 实现的高性能轻量级代理库,支持类、接口、同步方法、异步方法拦截。
安装
dotnet add package ProxyDispatch
快速入门
我们在主页上有不少例子,这是让您入门的第一个:
- 定义代理拦截器,并实现
AspectDispatchProxy
抽象方法:
public class YourClassProxy : AspectDispatchProxy
{
// 拦截同步
public override object Invoke(Invocation invocation)
{
Console.WriteLine($"~~~调用同步方法:{invocation.Method}");
var result = invocation.Proceed();
Console.WriteLine($"~~~调用同步方法结束,返回值:{result}");
return result;
}
// 拦截异步
public override async Task InvokeAsync(Invocation invocation)
{
Console.WriteLine($"~~~调用异步 Task 方法:{invocation.Method}");
await invocation.ProceedAsync();
Console.WriteLine("~~~调用异步 Task 方法结束");
}
// 拦截异步带返回值
public override async Task<T> InvokeAsync<T>(Invocation invocation)
{
Console.WriteLine($"~~~调用异步 Task<T> 方法:{invocation.Method}");
var result = await invocation.ProceedAsync<T>();
Console.WriteLine($"~~~调用异步 Task<T> 方法结束,返回值:{result}");
return result;
}
}
- 定义需要拦截的类和接口:
public class YourClass : IYourClass
{
// 同步方法
public string SyncMethod(string str)
{
return str;
}
// 异步方法
public async Task AsyncMethod()
{
await Task.CompletedTask;
}
// 异步方法带返回值
public async Task<int> AsyncMethodWithResult(int num)
{
await Task.Delay(10);
return num + 100;
}
// 泛型方法
public T GenericMethod<T>(T x)
{
return x;
}
}
public interface IYourClass
{
// 同步方法
string SyncMethod(string str);
// 异步方法
Task AsyncMethod();
// 异步方法带返回值
Task<int> AsyncMethodWithResult(int num);
// 泛型方法
T GenericMethod<T>(T x);
}
- 创建拦截代理对象:
// 创建拦截对象
var yourClass = AspectDispatchProxy.Decorate<IYourClass, YourClassProxy>(new YourClass());
yourClass.SyncMethod("百小僧");
await yourClass.AsyncMethod();
await yourClass.AsyncMethodWithResult(100);
Console.WriteLine("泛型方法 GenericMethod 调用,返回值:" + yourClass.GenericMethod("Furion"));
输出如下:
~~~调用同步方法:System.String SyncMethod(System.String)
~~~调用同步方法结束,返回值:百小僧
~~~调用异步 Task 方法:System.Threading.Tasks.Task AsyncMethod()
~~~调用异步 Task 方法结束
~~~调用异步 Task<T> 方法:System.Threading.Tasks.Task`1[System.Int32] AsyncMethodWithResult(Int32)
~~~调用异步 Task<T> 方法结束,返回值:200
泛型方法 GenericMethod 调用,返回值:Furion
- 还可以直接代理类,无需接口:
// 创建拦截对象(无依赖接口)
var yourClassNoInterface = AspectDispatchProxy.DecorateClass<YourClass, YourClassProxy>(new YourClass());
yourClassNoInterface.SyncMethod("百小僧");
await yourClassNoInterface.AsyncMethod();
await yourClassNoInterface.AsyncMethodWithResult(100);
Console.WriteLine("泛型方法 GenericMethod 调用,返回值:" + yourClassNoInterface.GenericMethod("Furion"));
文档
您可以在主页找到 ProxyDispatch 文档。
贡献
该存储库的主要目的是继续发展 ProxyDispatch 核心,使其更快、更易于使用。ProxyDispatch 的开发在 Gitee 上公开进行,我们感谢社区贡献错误修复和改进。
许可证
ProxyDispatch 采用 MIT 开源许可证。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- Microsoft.CodeAnalysis.CSharp (>= 4.6.0)
-
net6.0
- Microsoft.CodeAnalysis.CSharp (>= 4.6.0)
-
net7.0
- Microsoft.CodeAnalysis.CSharp (>= 4.6.0)
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.6.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.