Does Resolve<T>(name) return a default object if name not found?

Sep 21, 2009 at 11:48 PM

Should a call to Resolve<Foo>("MyFoo") return a default instance of Foo if no "MyFoo" is found in the Unity configuration section?  It appears that it does which wasn't what I expected.  When you try to resolve an interface such as IFoo, you'll of course get an exception if name isn't found since it doesn't know what to instantiate.  I thought the behaviour would be the same regard less of whether T was an interface or a class name.

If this is the expected behaviour, is there a way to determine if the name ("MyFoo") was found in the Unity configuration?

Thanks for any info!

Andrew

Sep 30, 2009 at 10:15 PM

When you register a concrete type, either with a name or without, it's really just specifing specific configuration for that type. In the absence of that specific configuration, Unity will still try to create the object use its default behavior - constructor injection only, call the longest constructor, and use attributes. So it's working as designed.

At this time there's no direct way to query what types or names have been registered; that kind of container introspection is on the backlog for Unity 2.0.