本文整理汇总了C#中System.ComponentModel.Design.ServiceContainer.GetService方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.GetService方法的具体用法?C# ServiceContainer.GetService怎么用?C# ServiceContainer.GetService使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.ComponentModel.Design.ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.GetService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GeneralTest1
public void GeneralTest1 ()
{
ServiceContainer sc = new ServiceContainer ();
sc.AddService (typeof (Svc), new Svc());
Svc service1 = sc.GetService (typeof (Svc)) as Svc;
AssertNotNull ("GT1#01", service1);
AssertEquals ("GT1#02", service1, sc.GetService (typeof (Svc)));
AssertNull ("GT1#04", sc.GetService (typeof (NotInSvc)));
}
示例2: TestServiceCreator
public void TestServiceCreator ()
{
ServiceContainer sc = new ServiceContainer ();
sc.AddService(typeof(Svc), new ServiceCreatorCallback(Svc.ServiceCreator));
AssertNull ("TSC#01", sc.GetService (typeof(NotInSvc)));
Svc service1 = sc.GetService (typeof(Svc)) as Svc;
AssertNotNull ("TSC#02", service1);
AssertEquals ("TSC#03", Svc.TotalObjectsCreatedByCallback, 1);
Svc service2 = sc.GetService (typeof(Svc)) as Svc;
AssertEquals ("TSC#04", service2, service1);
AssertEquals ("TSC#05", Svc.TotalObjectsCreatedByCallback, 1);
}
示例3: CreateTransformationContextProvider
private static ITransformationContextProvider CreateTransformationContextProvider()
{
using (var container = new ServiceContainer())
{
TransformationContextProvider.Register(container);
return (ITransformationContextProvider)container.GetService(typeof(ITransformationContextProvider));
}
}
示例4: AddService1
public void AddService1 ()
{
object service;
ServiceContainer parent;
ServiceContainer sc;
object serviceInstance1 = new ArrayList ();
object serviceInstance2 = new Hashtable ();
object callback1 = new ServiceCreatorCallback (
Svc.ServiceCreator);
sc = new ServiceContainer ();
sc.AddService (typeof (ICollection), serviceInstance1);
sc.AddService (typeof (IEnumerable), serviceInstance2);
sc.AddService (typeof (Svc), callback1);
service = sc.GetService (typeof (ICollection));
Assert.IsNotNull (service, "#A1");
Assert.AreSame (serviceInstance1, service, "#A2");
service = sc.GetService (typeof (IEnumerable));
Assert.IsNotNull (service, "#B1");
Assert.AreSame (serviceInstance2, service, "#B2");
service = sc.GetService (typeof (ArrayList));
Assert.IsNull (service, "#C1");
service = sc.GetService (typeof (ICloneable));
Assert.IsNull (service, "#D1");
Assert.AreEqual (0, Svc.TotalObjectsCreatedByCallback, "#E1");
service = sc.GetService (typeof (Svc));
Assert.IsNotNull (service, "#E2");
Assert.IsTrue (service is Svc, "#E3");
Assert.AreEqual (1, Svc.TotalObjectsCreatedByCallback, "#E4");
Assert.AreSame (service, sc.GetService (typeof (Svc)), "#E5");
Assert.AreEqual (1, Svc.TotalObjectsCreatedByCallback, "#E6");
parent = new ServiceContainer ();
sc = new ServiceContainer (parent);
sc.AddService (typeof (ICollection), serviceInstance1);
Assert.AreSame (serviceInstance1, sc.GetService (typeof (ICollection)), "#F1");
Assert.IsNull (parent.GetService (typeof (ICollection)), "#F2");
}
示例5: TestServiceCreator
public void TestServiceCreator ()
{
ServiceContainer sc = new ServiceContainer ();
sc.AddService (typeof(Svc), new ServiceCreatorCallback (Svc.ServiceCreator));
Assert.IsNull (sc.GetService (typeof (NotInSvc)), "#A");
Svc service1 = sc.GetService (typeof (Svc)) as Svc;
Assert.IsNotNull (service1, "#B1");
Assert.AreEqual (1, Svc.TotalObjectsCreatedByCallback, "#B2");
Svc service2 = sc.GetService (typeof (Svc)) as Svc;
Assert.AreEqual (service1, service2, "#C1");
Assert.AreEqual (1, Svc.TotalObjectsCreatedByCallback, "#C2");
}
示例6: GetService_DefaultServices
public void GetService_DefaultServices ()
{
ServiceContainer sc1 = new ServiceContainer ();
Assert.AreSame (sc1, sc1.GetService (typeof (IServiceContainer)), "#A1");
Assert.AreSame (sc1, sc1.GetService (typeof(ServiceContainer)), "#A2");
ServiceContainer sc2 = new ServiceContainer ();
sc1.AddService (typeof (IServiceContainer), sc2);
sc1.AddService (typeof (ServiceContainer), sc2);
Assert.AreSame (sc1, sc1.GetService (typeof (IServiceContainer)), "#B1");
Assert.AreSame (sc1, sc1.GetService (typeof(ServiceContainer)), "#B2");
}
示例7: GeneralTest2
public void GeneralTest2 ()
{
ServiceContainer sc = new ServiceContainer ();
sc.AddService (typeof (Svc), new Svc ());
Svc service1 = sc.GetService (typeof (Svc)) as Svc;
Assert.IsNotNull (service1, "#A");
Assert.AreEqual (service1, sc.GetService (typeof (Svc)), "#2");
try {
sc.AddService (typeof (Svc), new Svc ());
Assert.Fail ("#B1");
} catch (ArgumentException ex) {
// The service MonoTests.System.ComponentModel.Design.Svc
// already exists in the service container
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsTrue (ex.Message.IndexOf (typeof (Svc).FullName) != -1, "#B5");
Assert.AreEqual ("serviceType", ex.ParamName, "#B6");
}
}
示例8: GeneralTest1
public void GeneralTest1 ()
{
ServiceContainer sc = new ServiceContainer ();
sc.AddService (typeof (Svc), new Svc ());
Svc service1 = sc.GetService (typeof (Svc)) as Svc;
Assert.IsNotNull (service1, "#1");
Assert.AreEqual (service1, sc.GetService (typeof (Svc)), "#2");
Assert.IsNull (sc.GetService (typeof (NotInSvc)), "#3");
}
示例9: AddService
[Test] // AddService (Type, Object, Boolean)
public void AddService3 ()
{
ServiceContainer sc;
ServiceContainer parent = new ServiceContainer ();
ArrayList serviceInstance1 = new ArrayList ();
ArrayList serviceInstance2 = new ArrayList ();
Type serviceType1 = typeof (IList);
Type serviceType2 = typeof (IEnumerable);
sc = new ServiceContainer (parent);
sc.AddService (serviceType1, serviceInstance1, true);
sc.AddService (serviceType2, serviceInstance2, false);
Assert.AreSame (serviceInstance1, parent.GetService (serviceType1), "#A1");
Assert.IsNull (parent.GetService (serviceType2), "#A2");
Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#A3");
Assert.AreSame (serviceInstance2, sc.GetService (serviceType2), "#A4");
sc = new ServiceContainer ();
sc.AddService (serviceType1, serviceInstance1, true);
sc.AddService (serviceType2, serviceInstance2, false);
Assert.AreSame (serviceInstance1, sc.GetService (serviceType1), "#B1");
Assert.AreSame (serviceInstance2, sc.GetService (serviceType2), "#B2");
parent = new ServiceContainer ();
sc = new ServiceContainer (parent);
sc.AddService (serviceType1, serviceInstance1, true);
sc.AddService (serviceType1, serviceInstance2, false);
Assert.AreSame (serviceInstance2, sc.GetService (serviceType1), "#C1");
Assert.AreSame (serviceInstance1, parent.GetService (serviceType1), "#C2");
}
示例10: BasicTest
public void BasicTest() {
Class1 class1 = new Class1();
Class2 class2 = new Class2();
ServiceContainer container = new ServiceContainer();
container.AddService(typeof(IInterface1), class1);
container.AddService(typeof(IInterface2), class2);
Assert.AreSame(class1, container.GetService(typeof(IInterface1)));
Assert.AreSame(class2, container.GetService(typeof(IInterface2)));
Assert.IsNull(container.GetService(typeof(IInterface3)));
}
示例11: MockObjectsMayBePlacedIntoServiceContainers
public void MockObjectsMayBePlacedIntoServiceContainers()
{
var container = new ServiceContainer();
var mockedType = Mockery.NewInstanceOfRole(typeof (IMockedType)) as IMockedType;
container.AddService(typeof (IMockedType), mockedType);
Assert.AreSame(mockedType, container.GetService(typeof (IMockedType)));
}
示例12: ParentService
public void ParentService ()
{
ServiceContainer scParent = new ServiceContainer ();
ServiceContainer sc = new ServiceContainer (scParent);
scParent.AddService(typeof(Svc), new Svc ());
Svc service1 = sc.GetService (typeof (Svc)) as Svc;
Assert.IsNotNull (service1, "#1");
}
示例13: 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)));
}
示例14: GetService_DefaultServices
public void GetService_DefaultServices ()
{
ServiceContainer sc1 = new ServiceContainer ();
Assert.AreSame (sc1, sc1.GetService (typeof (IServiceContainer)), "#A1");
#if NET_2_0
Assert.AreSame (sc1, sc1.GetService (typeof(ServiceContainer)), "#A2");
#else
Assert.IsNull (sc1.GetService (typeof (ServiceContainer)), "#A2");
#endif
ServiceContainer sc2 = new ServiceContainer ();
sc1.AddService (typeof (IServiceContainer), sc2);
sc1.AddService (typeof (ServiceContainer), sc2);
Assert.AreSame (sc1, sc1.GetService (typeof (IServiceContainer)), "#B1");
#if NET_2_0
Assert.AreSame (sc1, sc1.GetService (typeof(ServiceContainer)), "#B2");
#else
Assert.AreSame (sc2, sc1.GetService (typeof (ServiceContainer)), "#B2");
#endif
}
示例15: Initialize
private void Initialize()
{
IDesignerHost host;
Form form;
IRootDesigner rootDesigner;
Control view;
// Initialise service container and designer host
serviceContainer = new ServiceContainer();
serviceContainer.AddService(typeof(INameCreationService), new NameCreationService());
serviceContainer.AddService(typeof(IUIService), new UIService(this));
host = new DesignerHost(serviceContainer);
// Add toolbox service
serviceContainer.AddService(typeof(IToolboxService), lstToolbox);
lstToolbox.designPanel = pnlViewHost;
PopulateToolbox(lstToolbox);
// Add menu command service
menuService = new MenuCommandService();
serviceContainer.AddService(typeof(IMenuCommandService), menuService);
// Start the designer host off with a Form to design
form = (Form)host.CreateComponent(typeof(Form));
form.TopLevel = false;
form.Text = "Form1";
// Get the root designer for the form and add its design view to this form
rootDesigner = (IRootDesigner)host.GetDesigner(form);
view = (Control)rootDesigner.GetView(ViewTechnology.WindowsForms);
view.Dock = DockStyle.Fill;
pnlViewHost.Controls.Add(view);
// Subscribe to the selectionchanged event and activate the designer
ISelectionService s = (ISelectionService)serviceContainer.GetService(typeof(ISelectionService));
s.SelectionChanged += new EventHandler(OnSelectionChanged);
host.Activate();
}