本文整理汇总了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");
}
}
示例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)));
}