當前位置: 首頁>>代碼示例>>C#>>正文


C# ServiceContainer.RemoveService方法代碼示例

本文整理匯總了C#中System.ComponentModel.Design.ServiceContainer.RemoveService方法的典型用法代碼示例。如果您正苦於以下問題:C# ServiceContainer.RemoveService方法的具體用法?C# ServiceContainer.RemoveService怎麽用?C# ServiceContainer.RemoveService使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.ComponentModel.Design.ServiceContainer的用法示例。


在下文中一共展示了ServiceContainer.RemoveService方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: RemoveService

		[Test] // RemoveService (Type, Boolean)
		public void RemoveService2_ServiceType_Null ()
		{
			ServiceContainer sc = new ServiceContainer ();

			try {
				sc.RemoveService ((Type) null, false);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("serviceType", ex.ParamName, "#A5");
			}

			try {
				sc.RemoveService ((Type) null, true);
				Assert.Fail ("#B1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.AreEqual ("serviceType", ex.ParamName, "#B5");
			}
		}
開發者ID:Profit0004,項目名稱:mono,代碼行數:25,代碼來源:ServiceContainerTest.cs

示例2: NestedContainers

        public void NestedContainers() {
            Class1 class1 = new Class1();
            Class2 class2 = new Class2();
            Class3 class3 = new Class3();
            Class1 class1a = new Class1();

            ServiceContainer parentContainer = new ServiceContainer();
            parentContainer.AddService(typeof(IInterface1), class1);

            ServiceContainer childContainer = new ServiceContainer(parentContainer);
            childContainer.AddService(typeof(IInterface2), class2);

            // child container returns what it contains and what its parent contains
            Assert.AreSame(class1, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class2, childContainer.GetService(typeof(IInterface2)));
            Assert.IsNull(childContainer.GetService(typeof(IInterface3)));

            // parent container only returns what it contains
            Assert.IsNull(parentContainer.GetService(typeof(IInterface2)));

            // add a service to the parent, and it becomes available to the child
            parentContainer.AddService(typeof(IInterface3), class3);
            Assert.AreSame(class3, childContainer.GetService(typeof(IInterface3)));
            Assert.AreSame(class3, parentContainer.GetService(typeof(IInterface3)));

            // remove a service from the parent, and it is no longer available to the child
            parentContainer.RemoveService(typeof(IInterface3));
            Assert.IsNull(childContainer.GetService(typeof(IInterface3)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface3)));

            // remove a service from the child container but not the parent container
            childContainer.RemoveService(typeof(IInterface1));
            Assert.AreSame(class1, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1, parentContainer.GetService(typeof(IInterface1)));

            // add an implementation to the child container and it overrides the parent container
            childContainer.AddService(typeof(IInterface1), class1a);
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1, parentContainer.GetService(typeof(IInterface1)));

            // remove implementation from the child container, and the implementation in the parent
            // container is used again in the child container
            childContainer.RemoveService(typeof(IInterface1));
            Assert.AreSame(class1, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1, parentContainer.GetService(typeof(IInterface1)));

            // remove implementation in the child container using promotion 
            childContainer.RemoveService(typeof(IInterface1), true);
            Assert.IsNull(childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));

            // add implementation to both parent and child container, and then remove from child using
            // promotion, the implementation left in the child will remain
            parentContainer.AddService(typeof(IInterface1), class1);
            childContainer.AddService(typeof(IInterface1), class1a);
            childContainer.RemoveService(typeof(IInterface1), true);
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));

            // remove using promotion again and the implementation remains in the child
            childContainer.RemoveService(typeof(IInterface1), true);
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.AreSame(class1a, childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));

            // remove without promotion and the instance is gone in the child
            childContainer.RemoveService(typeof(IInterface1), false);
            Assert.IsNull(childContainer.GetService(typeof(IInterface1)));
            Assert.IsNull(parentContainer.GetService(typeof(IInterface1)));
        }
開發者ID:matthewc-mps-aust,項目名稱:quokka,代碼行數:70,代碼來源:ServiceProviderTests.cs


注:本文中的System.ComponentModel.Design.ServiceContainer.RemoveService方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。