问题描述
|
我是企业图书馆的新手。我想问一些问题,我们将不胜感激。
1,如何部署注入实例属性。
public class MyObject
{
public MyObject(string Title)
{
///...
}
public MyObject(InjectObject injectObject)
{
///...
}
public InjectObject InjectObject{get;set;}
public List<string> MyList {get;set;}
public string Title {get;set;}
}
Now I kNow how to inject the default value to the title property. But how to do
with
the InjectObject and the MyList.
<register type=\"IMyObject\" mapTo=\"MyObject\">
<property name=\"Title\" value=\"MyFirstObject\">
</property>
//But how to assign or instance the InjectObject and the MyList
</register>
But how to assign or instance the InjectObject and the MyList
<register type=\"IMyObject\" MapTo=“MyObject”>
<constructor>
<param type=\"string\" name=\"title\" value=\"MyFirstObject\"/>
</constructor>
2,如何将类实例传递给构造函数
而且我知道如何为构造函数分配字符串值。但是如何转移
一个类实例。
如何将类实例分配给构造函数,以及如何分配两个构造函数方法。
谢谢您的帮助。
最好的祝福。
戴夫
解决方法
首先,优先考虑构造函数注入而不是属性注入。
要将类型注入构造函数,请使用
<dependency [name=\"\"] />
属性。
例如:
<register type=\"IMyObject\" MapTo=“MyObject”>
<constructor>
<param name=\"injectObject\">
<dependency />
</param>
</constructor>
<register>
<register type=\"InjectObject\" />
更新:
要将数组添加为注入值,您需要配置以下内容:
<param name=\"parmName\">
<array>
<value value=\"firstValue\" />
<dependency />
<value value=\"some other value\" />
</array>
</param>
有关如何执行此操作的所有详细信息,请查看Unity配置模式。