使用Microsoft Unity进行程序化属性注入 [英] Programatic property injection with Microsoft Unity

查看:75
本文介绍了使用Microsoft Unity进行程序化属性注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决方案中使用了构造函数注入,但是这一类具有我不希望在具有不变依赖项的构造函数中传递的属性.

I use contructor injection in my solution, but this one class has a property that i do not want to pass in the constructor where i have the invariant dependencies.

比方说,我有一个ILogger,它具有要设置的FileName属性,同时仍然让它在构造函数中设置了相关性. 如何注册类型,并同时传递默认连接字符串.

Let's say i got an ILogger and it has a FileName property i want to set, while still having it set the dependancies in the contructor. How do i go about registering the type, and at the same time pass the defaunt connection string.

我希望有一种简单的方法-最好不使用属性装饰属性,但是如果使用属性设置起来更简单,我想那很酷:)

I hope there is an easy way to do it - preferably without decorating the property with an attribute, but if the setup is easier with the attribute i guess that's cool :)

问题是,如何在Unity中也使用构造函数注入的对象上注入属性值.

So the question is, how do i inject a property value on an object that also uses contructor injection - with Unity.

更新:我在标题中提到了它,但是我忘了在正文中进行详细说明-我想手动(通过代码)设置这些依赖关系,而不是在配置文件中设置.

UPDATE: I mentioned it in the title, but i forgot to elaborate in the body of the text - i want to set these dependencies up manually (in code) as opposed to in a config file.

推荐答案

好吧,我想这有助于提出这个问题,我发现-在这里.

Ok i guess it helped to ask the question, i found out - here it is.

container.Configure<InjectedMembers>().ConfigureInjectionFor<BasicLogger>(
  new InjectionProperty("FileName", @"C:\test.log")
);

这篇关于使用Microsoft Unity进行程序化属性注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆