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


C# UnityContainer.Teardown方法代码示例

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


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

示例1: Combined_Test

        public void Combined_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            container.RegisterType<DisposableClass>("transient", new DisposingTransientLifetimeManager());
            container.RegisterType<DisposableClass>("shared", new DisposingSharedLifetimeManager());

            var transient1 = container.Resolve<DisposableClass>("transient");
            var transient2 = container.Resolve<DisposableClass>("transient");
            Assert.AreNotEqual(transient1, transient2);

            var shared1 = container.Resolve<DisposableClass>("shared");
            Assert.AreNotEqual(transient1, shared1);
            Assert.AreNotEqual(transient2, shared1);

            var shared2 = container.Resolve<DisposableClass>("shared");
            Assert.AreEqual(shared1, shared2);

            container.Teardown(transient1);
            Assert.IsTrue(transient1.Disposed);

            container.Teardown(shared2);
            Assert.IsFalse(shared2.Disposed);

            container.Teardown(shared1);
            Assert.IsTrue(shared1.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:28,代码来源:CombinedTests.cs

示例2: UnityWithConfiguration

        private void UnityWithConfiguration()
        {
            // Create and populate a new Unity container from configuration            
            UnityConfigurationSection UnityConfigurationSectionObject = (UnityConfigurationSection)ConfigurationManager.GetSection("UnityBlockConfiguration");
            IUnityContainer UnityContainerObject = new UnityContainer();

            UnityConfigurationSectionObject.Containers["ContainerOne"].Configure(UnityContainerObject);
            Console.WriteLine("\nRetrieved the populated Unity Container.");

            // Get the logger to write a message and display the result. No name in config file.
            ILogger ILoggerInstance = UnityContainerObject.Resolve < ILogger>();            
            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO Default UNITY!"));

            // Resolve an instance of the appropriate class registered for ILogger 
            // Using the specified mapping name in the configuration file (may be empty for the default mapping)
            ILoggerInstance = UnityContainerObject.Resolve<ILogger>("StandardLoggerMappedInConfig");
            
            // Get the logger to write a message and display the result
            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO StandardLogger!"));
            
            // Resolve an instance of the appropriate class registered for ILogger 
            // Using the specified mapping name (may be empty for the default mapping)
            ILoggerInstance = UnityContainerObject.Resolve<ILogger>("SuperFastLoggerMappedInConfig");
            
            // Get the logger to write a message and display the result
            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO SuperFastLogger!"));

            // Constructor Injection.
            // Resolve an instance of the concrete MyObjectWithInjectedLogger class 
            // This class contains a reference to ILogger in the constructor parameters 
            MyObjectWithInjectedLogger MyObjectWithInjectedLoggerInstance = UnityContainerObject.Resolve<MyObjectWithInjectedLogger>();
            // Get the injected logger to write a message and display the result
            Console.WriteLine("\n" + MyObjectWithInjectedLoggerInstance.GetObjectStringResult());

            // Throws error as we are trying to create instance for interface.
            //IMyInterface IMyInterfaceObject = UnityContainerObject.Resolve<IMyInterface>();

            IMyInterface IMyInterfaceObject = UnityContainerObject.Resolve<IMyInterface>();
            Console.WriteLine("\n" + IMyInterfaceObject.GetObjectStringResult());

            //If we are not sure whether a named registration exists for an object, 
            // we can use the ResolveAll method to obtain a list of registrations and mappings, and then check for the object we need in the list returned by this method. 
            // However, this will cause the container to generate all the objects for all named registrations for the specified object type, which will affect performance.
            IEnumerable<object> IEnumerableObjects = UnityContainerObject.ResolveAll(typeof(ILogger));
            int i = 0;
            foreach (ILogger foundObject in IEnumerableObjects)
            {
                // Convert the object reference to the "real" type
                ILogger theObject = foundObject as ILogger;
                i++;
                if (null != theObject)
                {
                    Console.WriteLine(theObject.WriteMessage("Reading Object " + i.ToString()));
                }
            }
            
            UnityContainerObject.Teardown(IMyInterfaceObject);

            Console.ReadLine();
        }
开发者ID:mac10688,项目名称:PublicWorkspace,代码行数:60,代码来源:Program.cs

示例3: MassDisposable_Test

        public void MassDisposable_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();
            container.RegisterType<DisposableClass>(new DisposingTransientLifetimeManager());

            var sw = new Stopwatch();
            sw.Start();
            var objects = new List<DisposableClass>();
            for (int i = 0; i < 10000; i++)
            {
                objects.Add(container.Resolve<DisposableClass>());
            }

            sw.Stop();
            Context.WriteLine("Resolve: " + sw.Elapsed.ToString());

            sw.Restart();
            for (int i = 0; i < 10000; i++)
            {
                container.Teardown(objects[i]);
                Assert.IsTrue(objects[i].Disposed);
            }

            sw.Stop();
            Context.WriteLine("Teardown: " + sw.Elapsed.ToString());
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:27,代码来源:DisposingTransientLifetimeManagerTests.cs

示例4: TransientParentWithSharedChild_Test

        public void TransientParentWithSharedChild_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            container.RegisterType<ClassWithDisposable>(new DisposingTransientLifetimeManager());
            container.RegisterType<DisposableClass>(new DisposingSharedLifetimeManager());

            var obj1 = container.Resolve<ClassWithDisposable>();
            var obj2 = container.Resolve<ClassWithDisposable>();

            container.Teardown(obj1);
            Assert.IsFalse(obj2.Disposable.Disposed);

            container.Teardown(obj2);
            Assert.IsTrue(obj2.Disposable.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:17,代码来源:CombinedTests.cs

示例5: ClassWithTwoDisposables_Test

        public void ClassWithTwoDisposables_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            container.RegisterType<DisposableClass>(new DisposingSharedLifetimeManager());

            var obj1 = container.Resolve<ClassWithTwoDisposables>();
            Assert.AreEqual(obj1.Disposable, obj1.Disposable2);

            var obj2 = container.Resolve<ClassWithTwoDisposables>();
            Assert.AreEqual(obj1.Disposable, obj2.Disposable);
            Assert.AreEqual(obj1.Disposable, obj2.Disposable2);
            Assert.AreNotEqual(obj1, obj2);

            container.Teardown(obj1);
            Assert.IsFalse(obj1.Disposable.Disposed);

            container.Teardown(obj2);
            Assert.IsTrue(obj2.Disposable.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:21,代码来源:SharedResourceLifetimeManagerTests.cs

示例6: UnityAtRuntime

        private void UnityAtRuntime()
        {
            // Create a new Unity container
            IUnityContainer UnityContainerObject = new UnityContainer();
            Console.WriteLine("\nRetrieved the populated Unity Container.");

            // Resolve an instance (Create an object) of the class registered for ILogger.
            // And inejcts the object which specified in config file container section.
            ILogger ILoggerObject = UnityContainerObject.Resolve<MyLogger>();

            // Invoke the method from Injected object and display the result
            Console.WriteLine("\n" + ILoggerObject.WriteMessage("My Logger"));

            UnityContainerObject.Teardown(ILoggerObject);

            
            // Register a new default (un-named) mapping with the container
            // This will replaces the existing mapping in the container for ILogger            
            UnityContainerObject.RegisterType<ILogger, MyFastLogger>();
            Console.WriteLine("\nRegistered a new mapping for IMyInterface to MyOtherObject.");

            ILoggerObject = UnityContainerObject.Resolve<ILogger>();
            Console.WriteLine("\n" + ILoggerObject.WriteMessage("Fast Logger"));

            // Resolve an instance of the appropriate class registered for ILogger 
            // Using the specified mapping name in the configuration file (may be empty for the default mapping)
            ILogger ILoggerInstance = UnityContainerObject.Resolve<ILogger>("StandardLogger");

            // Get the logger to write a message and display the result
            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO UNITY!"));

            // Resolve an instance of the appropriate class registered for ILogger 
            // Using the specified mapping name (may be empty for the default mapping)
            ILoggerInstance = UnityContainerObject.Resolve<ILogger>("SuperFastLogger");            // This name specifies the type of logger required

            // Get the logger to write a message and display the result
            Console.WriteLine("\n" + ILoggerInstance.WriteMessage("HELLO UNITY!"));

            // Resolve an instance of the concrete MyObjectWithInjectedLogger class 
            // This class contains a reference to ILogger in the constructor parameters
            MyObjectWithInjectedLogger MyObjectWithInjectedLoggerInstance = UnityContainerObject.Resolve<MyObjectWithInjectedLogger>();
            
            // Get the injected logger to write a message and display the result
            Console.WriteLine("\n" + MyObjectWithInjectedLoggerInstance.GetObjectStringResult());

            Console.ReadLine();
        }
开发者ID:mac10688,项目名称:PublicWorkspace,代码行数:47,代码来源:Program.cs

示例7: CreateNewAfterDispose_Test

        public void CreateNewAfterDispose_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            container.RegisterType<DisposableClass>(new DisposingSharedLifetimeManager());

            var obj1 = container.Resolve<DisposableClass>();
            container.Teardown(obj1);
            Assert.IsTrue(obj1.Disposed, "After teardown of only instance, object should be disposed.");

            var obj2 = container.Resolve<DisposableClass>();
            Assert.AreNotEqual(obj1, obj2, "Object was previously disposed, new instance should be created.");

            var obj3 = container.Resolve<DisposableClass>();
            Assert.AreEqual(obj2, obj3, "Second reference to the same object expected.");
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:17,代码来源:SharedResourceLifetimeManagerTests.cs

示例8: ForgottenTeardown_Test

        public void ForgottenTeardown_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            container.RegisterType<DisposableClass>(new DisposingSharedLifetimeManager());

            var obj1 = container.Resolve<ClassWithDisposable>();
            var obj2 = container.Resolve<ClassWithDisposable>();
            var obj3 = container.Resolve<ClassWithDisposable>();

            var wr = new WeakReference(obj1);
            obj1 = null;

            GC.Collect();
            Assert.IsFalse(wr.IsAlive);

            container.Teardown(obj2);
            Assert.IsFalse(obj2.Disposable.Disposed);

            container.Teardown(obj3);
            Assert.IsTrue(obj2.Disposable.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:23,代码来源:SharedResourceLifetimeManagerTests.cs

示例9: TeardownParsleyAsCachedDisposesParsleyWhenCacheLifetimeStrategyExtensionIsAdded

        public void TeardownParsleyAsCachedDisposesParsleyWhenCacheLifetimeStrategyExtensionIsAdded()
        {
            var container = new UnityContainer();
            container.AddNewExtension<CacheLifetimeStrategyExtension>();

            var lease = new SlidingLease(TimeSpan.FromTicks(1));
            var cache = new CacheLifetimeManager(lease);
            container.RegisterType<IIngredient, Parsley>(cache);

            var ingredient = container.Resolve<IIngredient>();

            Thread.Sleep(TimeSpan.FromMilliseconds(1));

            container.Teardown(ingredient);

            var parsley = Assert.IsAssignableFrom<Parsley>(ingredient);
            Assert.True(parsley.IsDisposed);
        }
开发者ID:mesta1,项目名称:dli.net_sourcecode,代码行数:18,代码来源:MenuFacts.cs

示例10: RegisteredAsDisposable_Test

        public void RegisteredAsDisposable_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();
            container.RegisterType<DisposableClass>(new DisposingTransientLifetimeManager());

            var obj1 = container.Resolve<DisposableClass>();
            var obj2 = container.Resolve<DisposableClass>();
            Assert.AreNotEqual(obj1, obj2);

            container.Teardown(obj1);
            Assert.IsTrue(obj1.Disposed);

            container.Teardown(obj2);
            Assert.IsTrue(obj2.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:16,代码来源:DisposingTransientLifetimeManagerTests.cs

示例11: TeardownParsleyAsPerResolveDoesNotDisposeParsley

        public void TeardownParsleyAsPerResolveDoesNotDisposeParsley()
        {
            var container = new UnityContainer();
            container.RegisterType<IIngredient, Parsley>(new PerResolveLifetimeManager());

            var ingredient = container.Resolve<IIngredient>();

            container.Teardown(ingredient);

            var parsley = Assert.IsAssignableFrom<Parsley>(ingredient);
            Assert.False(parsley.IsDisposed); // <-- Boo hiss!!
        }
开发者ID:mesta1,项目名称:dli.net_sourcecode,代码行数:12,代码来源:MenuFacts.cs

示例12: TeardownParsleyAsSingletonDoesNotDisposeParsley

        public void TeardownParsleyAsSingletonDoesNotDisposeParsley()
        {
            var container = new UnityContainer();
            container.RegisterType<IIngredient, Parsley>(new ContainerControlledLifetimeManager());

            var ingredient = container.Resolve<IIngredient>();

            container.Teardown(ingredient);

            var parsley = Assert.IsAssignableFrom<Parsley>(ingredient);
            Assert.False(parsley.IsDisposed);
        }
开发者ID:mesta1,项目名称:dli.net_sourcecode,代码行数:12,代码来源:MenuFacts.cs

示例13: MixedRegistrations_Test

        public void MixedRegistrations_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();
            container.RegisterType<DisposableClass>(new DisposingTransientLifetimeManager());
            container.RegisterType<DisposableClass>("transient");

            var disposingObj = container.Resolve<DisposableClass>();
            var disposingObj2 = container.Resolve<DisposableClass>("disposing");
            var transientObj = container.Resolve<DisposableClass>("transient");

            Assert.AreNotEqual(disposingObj, disposingObj2);
            Assert.AreNotEqual(disposingObj, transientObj);

            container.Teardown(disposingObj);
            Assert.IsTrue(disposingObj.Disposed);

            container.Teardown(disposingObj2);
            Assert.IsTrue(disposingObj.Disposed);

            container.Teardown(transientObj);
            Assert.IsFalse(transientObj.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:23,代码来源:DisposingTransientLifetimeManagerTests.cs

示例14: UnregisteredGenericParent_Test

        public void UnregisteredGenericParent_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            var obj1 = container.Resolve<GenericClass<SimpleClass>>();
            var obj2 = container.Resolve<GenericClass<DisposableClass>>();
            Assert.AreNotEqual(obj1, obj2);

            container.Teardown(obj1);
            container.Teardown(obj2);
            Assert.IsFalse(obj2.Property.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:13,代码来源:DisposingTransientLifetimeManagerTests.cs

示例15: UnregisteredClass_Test

        public void UnregisteredClass_Test()
        {
            var container = new UnityContainer();
            container.AddNewExtension<DisposableStrategyExtension>();

            var obj1 = container.Resolve<DisposableClass>();
            var obj2 = container.Resolve<DisposableClass>();
            Assert.AreNotEqual(obj1, obj2);

            container.Teardown(obj1);
            Assert.IsFalse(obj1.Disposed);

            container.Teardown(obj2);
            Assert.IsFalse(obj2.Disposed);
        }
开发者ID:mskichu,项目名称:UnityExtension,代码行数:15,代码来源:DisposingTransientLifetimeManagerTests.cs


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