@Enterprise Library Unity 属性注入
我是企业图书馆的新手。我想问一些问题,任何帮助将不胜感激。
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>
二、如何将类实例传递给构造函数 我知道如何将字符串值分配给构造函数。但如何转账 一个类实例。
如何将类实例分配给构造函数以及如果我有两个构造函数方法要部署,如何分配。
感谢您的帮助。 此致。
戴夫德
I'm a fresher with the enterprise library. I want to ask some questions and any help will be appreciate.
1、 How to deploy inject an instance property.
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、 How to deliver a class instance to the constructor
and I know how to assign a string value to the constructor. But how to transfer
a class instance.
How can I assign a class instance to the constructor and How if i have two constructor method to deploy.
Thank you for your help.
Best Regards.
Daivd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,更喜欢构造函数注入而不是属性注入。
要将类型注入构造函数,请使用
属性。例如:
更新:
要添加数组作为注入值,您需要配置如下内容:
查看 Unity 配置架构 了解如何执行此操作的所有详细信息。
Firstly, prefer constructor injection over property injection.
To inject the type to the constructor, you use the
<dependency [name=""] />
attribute.For example:
UPDATE:
To add an array as the injection value you need to configure something like this:
Check out the Unity configure schema for all the detail on how to do this.