https://github.com/stratosblue/dependencyinjectionstaticproxy
Create a static proxy for the service without changing the service implementation within the DI container; 在不更改 `DI` 容器内服务实现的前提下为服务创建静态代理;
https://github.com/stratosblue/dependencyinjectionstaticproxy
Last synced: 6 months ago
JSON representation
Create a static proxy for the service without changing the service implementation within the DI container; 在不更改 `DI` 容器内服务实现的前提下为服务创建静态代理;
- Host: GitHub
- URL: https://github.com/stratosblue/dependencyinjectionstaticproxy
- Owner: stratosblue
- License: mit
- Created: 2023-07-09T09:19:14.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-05T01:18:14.000Z (over 1 year ago)
- Last Synced: 2025-03-16T09:47:30.238Z (7 months ago)
- Language: C#
- Size: 24.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# DependencyInjectionStaticProxy
Service static proxy tool for DI container, Create a static proxy for the service without changing the service implementation within the 'DI' container;
`DI` 容器的服务静态代理工具,在不更改 `DI` 容器内服务实现的前提下为服务创建静态代理;
- 使用 `IReplacedServiceAccessor` 访问容器内的原始实例;
-------
### 使用示例
静态代理容器内的 `IConfiguration` 而不用关心其具体实现/不改变其原本逻辑#### 创建静态代理类
代理 `IConfiguration` 并在key被访问时输出到控制台
```C#
public class ProxiedConfiguration : IConfiguration
{
private readonly IConfiguration _configuration;public ProxiedConfiguration(IReplacedServiceAccessor replacedServiceAccessor)
{
_configuration = replacedServiceAccessor.Service;
}public string this[string key]
{
get => _configuration[AccessLog(key)];
set => _configuration[AccessLog(key)] = value;
}public IEnumerable GetChildren()
{
return _configuration.GetChildren();
}public IChangeToken GetReloadToken()
{
return _configuration.GetReloadToken();
}public IConfigurationSection GetSection(string key)
{
return _configuration.GetSection(AccessLog(key));
}public string AccessLog(string key)
{
Console.WriteLine($"\"{key}\" has been accessed.");
return key;
}
}
```#### 代理并替换服务容器内的 `IConfiguration`
```C#
services.ProxyReplace();
```