如果要实例化的类只有一个构造函数, 则使用方法很简单使用方法如下:

1
2
3
4
5
6
7
using (IUnityContainer container = new UnityContainer())
{
    UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    section.Configure(container);    //...
    ILogger logger = container.Resolve<ILogger>("DatabaseLogger");
    return logger;
}

其中配置文件为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity>
    <containers>
      <container>
        <types>
          <type type="Bery.ILogger, UnityStudy" mapTo="Bery.DatabaseLogger, UnityStudy" name="DatabaseLogger">
          </type>
        </types>
      </container>
    </containers>
  </unity>
</configuration>

如果DatabaseLogger类中的有两个构造函数, 代码如下

1
2
3
4
5
6
public DatabaseLogger()
}
public DatabaseLogger(string name)
{
}

则Unity自动使用参数最多的构造函数进行创建对象, 会抛出以下异常:

1
2
3
Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Bery.ILogger", name = "DatabaseLogger".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type String cannot be constructed. You must configure the container to supply this value.

如果您想让它使用无参的构造函数创建, 则要使用[InjectionConstructor]特性进行修饰无参的构造函数,

1
2
3
4
[InjectionConstructor]
public DatabaseLogger()
}

若您想使用带参数的构造函数创建对象, 除了在构造函数上使用[InjectionConstructor]外, 还要在创建时传递参数,代码如下

1
2
3
4
5
6
7
8
9
10
using (IUnityContainer container = new UnityContainer())
{
    UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
    section.Configure(container);
    ILogger logger = container.Resolve<ILogger>("DatabaseLogger",
        new ParameterOverrides{
        {"name", "logName"}
    });
    return logger;

引用地址:http://unity3d.9tech.cn/news/2014/0208/39766.html

Unity中使用多构造函数的更多相关文章

  1. 关于Unity中MonoBehaviour的构造函数

    关于Unity中MonoBehaviour的构造函数 在学习Unity MVVM UI框架的时候,一不小给一个继承自MonoBehaviour类的子类编写了自定义构造函数,结果调Bug调了两个钟,特此 ...

  2. Unity中使用多构造函数(转)

    如果要实例化的类只有一个构造函数, 则使用方法很简单使用方法如下: 1 2 3 4 5 6 7 using (IUnityContainer container = new UnityContaine ...

  3. unity中的构造函数

    避免使用构造函数 不要在构造函数中初始化任何变量,使用Awake或Start实现这个目的.即使是在编辑模式中Unity也自动调用构造函数,这通常发生在一个脚本被编译之后,因为需要调用构造函数来取向一个 ...

  4. WP8:在Unity中使用OpenXLive

    Unity 4.2正式版开始添加了对Windows 8.Windows Phone 8等其他平台的支持,而且开发者可以免费使用Unity引擎来开发游戏了.而作为Windows Phone和Window ...

  5. 【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则

    作者:Williammao, 腾讯移动客户端开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/290.h ...

  6. 从Unity中的Attribute到AOP(七)

    本章我们将依然讲解Unity中的Attribute,继续命名空间在UnityEngine里的. PropertyAttribute,这个特性主要来控制变量或者类在Inspector里面的显示方式.和P ...

  7. 从Unity中的Attribute到AOP(五)

    今天主要来讲一下Unity中带Menu的Attribute. 首先是AddComponentMenu.这是UnityEngine命名空间下的一个Attribute. 按照官方文档的说法,会在Compo ...

  8. 从Unity中的Attribute到AOP(三)

    上一篇我们对系统的Attributes进行了MSIL代码的查看,了解到了其本质就是一个类的构造函数.本章我们将编写自己的Attributes. 首先我们定义书的属性代码,如下: [AttributeU ...

  9. Unity中C#单例模式使用总结

    一.单例模式优点 单例模式核心在于对于某个单例类,在系统中同时只存在唯一一个实例,并且该实例容易被外界所访问: 意味着在内存中,只存在一个实例,减少了内存开销: 二.单例模式特点 只存在唯一一个实例: ...

随机推荐

  1. 求交集,差集,并集,善用java的set

    当有题目有求这些结果时,使用集合数据结构还是很快的.需要考虑的是,注意map和set的区别. public static void main(String[] args) { Set<Integ ...

  2. Php和httpd.conf的配置

    http://www.cnblogs.com/homezzm/archive/2012/08/01/2618062.html http://book.51cto.com/art/201309/4096 ...

  3. 表单提交中记得form表单放到table外面

    帝国后台按栏目搜索文章时怎么都不生效  控制台查看原来是 栏目的select的值没有提交过去,原来由于form标签在table标签里面,导致js生成的<select>标签提交失败. 解决办 ...

  4. shell执行php文件传递参数

    php -f index.php hello test 2314 shell命令执行php文件不像http那样通过GET方式传参 同样php文件获取的时候也不能用$_GET方法了 而是通过$argv[ ...

  5. javascript 中{}和[] 的理解

    下面的一段解释是摘抄的,基本理解正确,做个记录.其实js中数组其实就是对象,typeof(['a', 'b', 'c'])//测试之后结果为 :  "object" 一.{ } 大 ...

  6. gridview如何隐藏一列数据,但又可以使用这列数据

    解决方案在RowCreated事件中书写如下代码 void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    {       ...

  7. oracle数据库查询常用语句

    1.查询SCOTT表中有多少表,并显示表的一些描述select * from all_tables WHERE owner='SCOTT' ; 2.查询oracle数据库版本select * from ...

  8. python的一些语法糖

    1   Python中if-else语句的多种写法 a, b, c = 1, 2, 3 1.常规 if a>b: c = a else: c = b 2.表达式 c = a if a>b  ...

  9. springMVC获取数据--注意post方法会出现中文乱码问题

    1. 新建web project 2. 加入jar 3. 改写web.xml <?xml version="1.0" encoding="UTF-8"?& ...

  10. BNU OJ 50999 BQG's Approaching Deadline

    #include<cstdio> #include<algorithm> using namespace std; +; struct Homework { long long ...