当前位置: 首页>>代码示例>>C#>>正文


C# ServiceContainer.CreateChildContainer方法代码示例

本文整理汇总了C#中ServiceContainer.CreateChildContainer方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.CreateChildContainer方法的具体用法?C# ServiceContainer.CreateChildContainer怎么用?C# ServiceContainer.CreateChildContainer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceContainer的用法示例。


在下文中一共展示了ServiceContainer.CreateChildContainer方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ChildContainerShouldBeDisposed

 public void ChildContainerShouldBeDisposed()
 {
     var ioc = new ServiceContainer();
       var childContainer = ioc.CreateChildContainer();
       ioc.Dispose();
       Assert.That(() => childContainer.Clear(), Throws.TypeOf(typeof(ObjectDisposedException)));
 }
开发者ID:Zolniu,项目名称:DigitalRune,代码行数:7,代码来源:ServiceContainerTest.cs

示例2: ComplexHierarchy

        public void ComplexHierarchy()
        {
            // Build hierarchy.
              var root = new ServiceContainer();
              var child1 = root.CreateChildContainer();
              child1.Register(typeof(IService1), "token1", typeof(Service1), CreationPolicy.Shared);

              // Register root services after child1 is created to see whether new services
              // are properly inherited.
              root.Register(typeof(IService1), null, typeof(Service1), CreationPolicy.Shared);
              root.Register(typeof(IService2), null, typeof(Service2), CreationPolicy.LocalShared);
              root.Register(typeof(IService3), null, typeof(Service3), CreationPolicy.NonShared);

              var child2 = child1.CreateChildContainer();
              child2.Register(typeof(IService1), null, typeof(Service1), CreationPolicy.Shared);
              child2.Register(typeof(IService1), "token2", typeof(DisposableService), CreationPolicy.Shared);

              // Resolve services.
              var service1InChild2 = child2.GetInstance<IService1>();
              var service2InChild2 = child2.GetInstance<IService2>();
              var service3InChild2 = child2.GetInstance<IService3>();
              var disposableServiceInChild2 = child2.GetInstance<IService1>("token2");

              var service3InChild1 = child1.GetInstance<IService3>();
              var service2InChild1 = child1.GetInstance<IService2>();
              var service1InChild1 = child1.GetInstance<IService1>();
              var namedService1InChild1 = child1.GetInstance<IService1>("token1");
              var namedService1InChild2 = child2.GetInstance<IService1>("token1");

              var service1InRoot = root.GetInstance<IService1>();
              var service3InRoot = root.GetInstance<IService3>();
              var service2InRoot = root.GetInstance<IService2>();

              // Check root services.
              Assert.IsNotNull(service1InRoot);
              Assert.IsNotNull(service2InRoot);
              Assert.IsNotNull(service3InRoot);
              Assert.IsNull(root.GetInstance<IService1>("token1"));
              Assert.AreSame(root, root.GetInstance<ServiceContainer>());
              Assert.AreSame(service1InRoot, root.GetInstance<IService1>());
              Assert.AreSame(service2InRoot, root.GetInstance<IService2>());
              Assert.AreNotSame(service3InRoot, root.GetInstance<IService3>());
              Assert.AreSame(service1InRoot, service3InRoot.Service1);
              Assert.AreSame(service2InRoot, service3InRoot.Service2);

              Assert.IsNull(service2InRoot.Service1);
              root.ResolveProperties(service2InRoot);
              Assert.AreSame(service1InRoot, service2InRoot.Service1);

              // Check child1 services.
              Assert.IsNotNull(service1InChild1);
              Assert.IsNotNull(service2InChild1);
              Assert.IsNotNull(service3InChild1);
              Assert.IsNotNull(namedService1InChild1);
              Assert.AreSame(child1, child1.GetInstance<ServiceContainer>());
              Assert.AreSame(service1InChild1, child1.GetInstance<IService1>());
              Assert.AreSame(service2InChild1, child1.GetInstance<IService2>());
              Assert.AreNotSame(service3InChild1, child1.GetInstance<IService3>());
              Assert.AreSame(service1InChild1, service3InChild1.Service1);
              Assert.AreSame(service2InChild1, service3InChild1.Service2);

              Assert.IsNull(service2InChild1.Service1);
              child1.ResolveProperties(service2InChild1);
              Assert.AreSame(service1InChild1, service2InChild1.Service1);

              Assert.AreSame(service1InRoot, service1InChild1);
              Assert.AreNotSame(service2InRoot, service2InChild1);

              // Check child2 services.
              Assert.IsNotNull(service1InChild2);
              Assert.IsNotNull(service2InChild2);
              Assert.IsNotNull(service3InChild2);
              Assert.IsNotNull(namedService1InChild2);
              Assert.IsNotNull(disposableServiceInChild2);
              Assert.AreSame(child2, child2.GetInstance<ServiceContainer>());
              Assert.AreSame(service1InChild2, child2.GetInstance<IService1>());
              Assert.AreSame(service2InChild2, child2.GetInstance<IService2>());
              Assert.AreNotSame(service3InChild2, child2.GetInstance<IService3>());
              Assert.AreSame(service1InChild2, service3InChild2.Service1);
              Assert.AreSame(service2InChild2, service3InChild2.Service2);

              Assert.IsNull(service2InChild2.Service1);
              child2.ResolveProperties(service2InChild2);
              Assert.AreSame(service1InChild2, service2InChild2.Service1);

              Assert.AreNotSame(service1InRoot, service1InChild2);
              Assert.AreNotSame(service1InChild1, service1InChild2);
              Assert.AreNotSame(service2InRoot, service2InChild2);
              Assert.AreNotSame(service2InChild1, service2InChild2);

              Assert.AreSame(child1.GetInstance<IService1>("token1"), child2.GetInstance<IService1>("token1"));
              Assert.AreSame(disposableServiceInChild2, child2.GetInstance<IService1>("token2"));

              var service1InstancesInChild2 = child2.GetAllInstances<IService1>().ToArray();
              Assert.AreEqual(2, service1InstancesInChild2.Length);
              Assert.IsTrue(service1InstancesInChild2.Contains(namedService1InChild1));
              Assert.IsTrue(service1InstancesInChild2.Contains(disposableServiceInChild2));

              // Unregister local entry.
              child2.Unregister(typeof(IService1));
//.........这里部分代码省略.........
开发者ID:Zolniu,项目名称:DigitalRune,代码行数:101,代码来源:ServiceContainerTest.cs

示例3: CreateChildContainer

        public void CreateChildContainer()
        {
            var ioc = new ServiceContainer();
              Assert.AreSame(ioc, ioc.Cast<object>().First());

              var childContainer = ioc.CreateChildContainer();
              Assert.AreSame(childContainer, childContainer.Cast<object>().First());
        }
开发者ID:Zolniu,项目名称:DigitalRune,代码行数:8,代码来源:ServiceContainerTest.cs


注:本文中的ServiceContainer.CreateChildContainer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。