Unity(3)

注:如类有共有的构造函数而且没有使用Dependency特性,可以在不需要注册映射关系的前提下,使用容器生成实例

1、获取默认映射

IMyService result = myContainer.Resolve<IMyService>();

2、获取命名映射

IMyService result = myContainer.Resolve<IMyService>("Data");

3、获取注册的所有对象

IEnumerable<IMyObject> objects = myContainer.ResolveAll<IMyObject>();

如果没有命名映射将返回Null

  foreach (IMyObject foundObject in objects) {

// convert the object reference to the "real" type

MyRealObject theObject = foundObject as MyRealObject;

if (null != theObject)

// work with the object

{

         Console.WriteLine(theObject.SomeProperty);

}

}

posted @ 2009-05-26 11:00  TG.Yang's IT Space  阅读(291)  评论(0编辑  收藏  举报