How does Unity.Resolve know which constructor to use?
2015-01-23 15:50
281 查看
Question:
Given a class with several constructors - how can I tell Resolve which constructor to use?
Consider the following example class:
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
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.
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.
相关文章推荐
- Know How To Use ID_NULL Function To Search An Object In Oracle Forms
- How to use unity CreateExternalTexture on Android?
- 如何解决sql server2005的“不允许远程连接”错误.(how to resolve the error: "SQL Server does not allow remote connections" for SQL Server2005)
- 802.11 WDS how does it work, when to use it and what are the limitations
- Unity: Passing Constructor Parameters to Resolve
- Vuforia How To Use Android Plugins in Unity Apps
- How does Gradle know whether to do a single or multiproject build?
- React Native Trying to resolve view with tag 518 which doesno't
- #include---I know how to use
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ? .
- How do I find which phpize to use?
- How Linkers Use Static Libraries to Resolve References
- Know How to Use Command-Line Tools
- General: Know How to Use InetAddress
- EF How to use context.Set and context.Entry, which ships with EF4.1 ?
- unity kinect2 怎么使用姿势或者使用你自己的How to Use Gestures or Create Your Own Ones
- How to use Autofac IoC Container with Unity 3D?
- how to solve "java.sql.Timestamp does not have a no-arg default constructor" ?
- how-to: resolve crontab does not work
- Know How to Use Command-line Tools