c# - 在ctor中带参数的Windsor依赖注入(inject)

标签 c# dependency-injection castle-windsor

我正在尝试配置 CaSTLe Windsor

我有一个FileReader实现的IFileReader接口(interface),FileReader在构造函数中有一个string属性

当我尝试使用 ServiceLocator 获取 FileReader 的实例时,如何设置此属性?

Container.Register(Component.For<IFileReader>().ImplementedBy<FileReader>());

public class FileReader
{
public FileReader(string fileName)
.
.
.

ServiceLocator.Current.GetInstance<IFileReader>(); <- how pass filename??

最佳答案

This link会帮助你。

简而言之,您可以使用DependsOnServiceOverridesParametersDynamicParameters。选择最适合您的。

关于c# - 在ctor中带参数的Windsor依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6382307/

相关文章:

c# - 在 .NET Compact Framework 3.5 的 HttpWebRequest 中指定客户端证书

c# - 如何在安装时(或在编译时轻松配置)配置 Windows 服务的名称?

c# - Visual Studio 2019 中的构建错误在错误列表中的显示不一致

c# - 实体类的依赖注入(inject)

caSTLe-windsor - 温莎城堡 : Using convention registration along with specific implementations

c# - 创建 BitmapImage 的背景

java - 从单元测试中注入(inject)模拟 Spring @Autowired 依赖项的最佳方法是什么?

spring - 为什么我不能将 Spring Environment 对象注入(inject)到我的 bean 中?

asp.net - 尝试模拟依赖项并向 IoC 容器注册时出现 System.InvalidOperationException

c# - CaSTLe Windsor 在程序集中找不到安装程序