https://github.com/li-xiaoyao/pureproxy
轻代理AOP,实现方法拦截以及入参返回值修改。
https://github.com/li-xiaoyao/pureproxy
csharp dotnet dotnetcore emit netcore proxy
Last synced: 3 months ago
JSON representation
轻代理AOP,实现方法拦截以及入参返回值修改。
- Host: GitHub
- URL: https://github.com/li-xiaoyao/pureproxy
- Owner: LI-XIAOYAO
- License: mit
- Created: 2022-11-03T13:13:45.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T15:06:07.000Z (almost 2 years ago)
- Last Synced: 2025-05-21T22:11:31.997Z (about 1 year ago)
- Topics: csharp, dotnet, dotnetcore, emit, netcore, proxy
- Language: C#
- Homepage:
- Size: 60.5 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# PureProxy
轻代理AOP,实现方法、属性拦截以及入参返回值修改。
---
#### 安装
> Install-Package PureProxy
#### Tips
- 忽略代理添加 `IgnoreProxyAttribute` 特性
- 局部拦截实现 `InterceptorAttribute` 特性,优先级:忽略 > 方法 > 类型 > 全局
- 类代理时需要定义为 `virtual`
#### 示例
````c#
// 添加全局拦截器
public class TestInterceptor : IInterceptor
{
public void Invoke(IArguments args)
{
// TODO...
// 修改入参
// args.Arguments[0] = "Test";
// 调用方法
var result = args.Invoke();
// TODO...
// 修改返回值
// args.Result = "Test";
}
}
// 局部拦截
public class ExceptionAttribute : InterceptorAttribute
{
public override void Invoke(IArguments arguments)
{
try
{
// TODO...
// 修改入参
// args.Arguments[0] = "Test";
// 调用方法
var result = args.Invoke();
// TODO...
// 修改返回值
// args.Result = "Test";
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
public class TestService : ITestService
{
[Exception]
public string Test()
{
throw new NotImplementedException();
}
}
// 添加注入
services.AddPureProxy(options =>
{
options.AddScoped();
options.AddScoped();
});
// 生成代理类型
var test3ServiceProxyType = PureProxyFactory.ProxyGenerator();
````