How does Unity.Resolve know which constructor to use?
Question:
Given a class with several constructors - how can I tell Resolve which constructor to use?
Consider the following example class:
public class Foo
{
public Foo() { }
public Foo(IBar bar)
{
Bar = bar;
}
public Foo(string name, IBar bar)
{
Bar = bar;
Name = name;
}
public IBar Bar { get; set; }
public string Name { get; set; }
}
If I want to create an object of type Foo using Resolve how will Resolve know which constructor to use? And how can I tell it to use the right one? Let's say I have a container with an IBar registered - will it understand that it should favor the constructor taking IBar? And if I specify a string too - will it use the (string, IBar) constructor?
Foo foo = unityContainer.Resolve<Foo>();
And please ignore the fact that it would probably be easier if the class just had a single constructor...
Answer:
When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more than one of the “longest” with the same number of parameters), Unity will raise an exception.
How does Unity.Resolve know which constructor to use?的更多相关文章
- Unity: Passing Constructor Parameters to Resolve
In this tutorial we will go through of couple different ways of using custom constructor parameters ...
- [IOC]Unity使用
Unity是什么? unity是patterns&practices团队开发的一个轻量级.可扩展的依赖注入容器. Unity特性 1.它提供了创建(或者装配)对象实例的机制,而这些对象实例可能 ...
- Unity学习笔记(1):认识Unity
Unity是什么? Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能 ...
- 理解依赖注入(IOC)和学习Unity
资料1: IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection). 作用:将各层的对象以松耦合的方式组织在一 ...
- IOC框架之 Unity 入门
十年河东,十年河西,莫欺少年穷 学无止境,精益求精 Unity是什么? Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 1. ...
- Unity是什么?
Unity是patterns & practices团队开发的一个轻量级.可扩展的依赖注入容器,具有如下的特性: 1. 它提供了创建(或者装配)对象实例的机制,而这些对象实例可能还包含了其它被 ...
- Unity3D C#脚本开发学习
1. Inherit from MonoBehaviour,All behaviour scripts must inherit from MonoBehaviour (directly or ind ...
- Enterprise Library 4.1 参考源码索引
http://www.projky.com/entlib/4.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...
- (转)Overview : Writing Scripts in C# 使用C#书写脚本
Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are: 除 ...
随机推荐
- 使用excel结合线性规划求解Holt-Winters参数
其实上面这个是Holt-Winters无季节趋势模型, 上面的S(t)对应下面的a(t)——截距(平滑值) b(t)仍然对应b(t)——趋势,T对应k. ...
- Like与Instr模糊查询性能
项目中用到like模糊查询,但是总觉的太小家子气,有没有高逼格的呢? instr(title,'手册')>0 相当于 title like '%手册%' instr(title,'手册')=1 ...
- 用jsp的application写一个记录用户登陆网站的数量
</head><body><%int i = 0;Object number = application.getAttribute("num");if ...
- 兼容IE8以下浏览器input表单属性placeholder不能智能提示功能
当前很多表单提示使用了表单属性placeholder,可这属性不兼容IE8以下的浏览器,我自己写了一个兼容处理js // 兼容IE8以下浏览器input不能智能提示功能 if(navigator.ap ...
- 生成解决方案,主项目的bin目录下没有其他项目生成的dll
问题说明: 我的项目组成: 主项目为:TaskUtlity 在生成解决方案的时候在TaskUtlity的bin目录下老是找不到ProBonus项目生成的dll. 解决方案: 1.打开sln文件,找到P ...
- HikariCP
HiKariCP是数据库连接池的一个后起之秀,号称性能最好,可以完美地PK掉其他连接池. 为何要使用HiKariCP?这要先从BoneCP说起: 什么?不是有C3P0/DBCP这些成熟的数据库连接池吗 ...
- 单节点下多个Tomcat服务器并存的端口号配置
一个服务器节点同时安装多个tomcat服务器时,如果仅仅修改访问端口号则会提示端口冲突启动失败,还需要修改另外端口号解决,一共需要修改3处地方,修改如下: 编辑配置文件:server.xml 1.首先 ...
- 3. UILable 的使用
1. 坐标系 来自:http://www.cnblogs.com/mcj-coding/p/5100455.html QQ: 863740091 如果在平面坐标系中我们要确定一个东西的位置和大小需 ...
- 模拟搭建Web项目的真实运行环境(一)
序言 最近尝试完整搭建一个Web项目的运行环境,总结一下这几个月学到的知识点. 后面的文章主要包括一下几个内容: A. 搭建一个Linux服务器,用来部署Redis.Mongo等数据存储环境: B. ...
- Hotaru's problem(hdu 5371)
题意:给出一个数字串,询问最长的子串,满足以下要求:将子串平均分为三部分,一三部分相等,一二部分对衬. /* 在manachar的基础上,枚举回文串的中心,再找第三部分. */ #include< ...