コード例 #1
0
        public void MyServiceContainerGetFirstServiceGeneric()
        {
            ServiceContainer parentServiceContainer = new ServiceContainer();
            ServiceContainer serviceContainer       = new ServiceContainer(parentServiceContainer);
            ServiceContainer serviceSubContainer    = new ServiceContainer();

            parentServiceContainer.AddService(new MyGenericServiceProvider <int>());
            parentServiceContainer.AddService(new MyGenericServiceProvider <int>());
            parentServiceContainer.AddService(new MyGenericServiceProvider <double>());
            parentServiceContainer.AddService(new MyAddService());
            serviceContainer.AddService(new MyReverseGenericServiceProvider <int>());
            serviceContainer.AddService(new MyGenericServiceProvider <int>());
            serviceContainer.AddService(new MyGenericServiceProvider <string>());
            serviceContainer.AddService(serviceSubContainer);
            serviceSubContainer.AddService(new MyReverseGenericServiceProvider <int>());
            serviceSubContainer.AddService(new MyReverseGenericServiceProvider <long>());
            serviceSubContainer.AddService(new MyGenericService2Provider <string, int>());
            serviceSubContainer.AddService(new MyGenericService2Provider <string, float>());

            IMyGenericListService <int> service1 = serviceContainer.GetService <IMyGenericListService <int> >();

            Assert.IsNotNull(service1);

            IMyGenericListService <double> service2 = serviceContainer.GetService <IMyGenericListService <double> >();

            Assert.IsNotNull(service2);

            IMyAddService service3 = serviceContainer.GetService <IMyAddService>();

            Assert.IsNotNull(service3);

            MyGenericServiceProvider <string> service4 = serviceContainer.GetService <MyGenericServiceProvider <string> >();

            Assert.IsNotNull(service4);

            MyReverseGenericServiceProvider <long> service5 = serviceContainer.GetService <MyReverseGenericServiceProvider <long> >();

            Assert.IsNotNull(service5);

            MyGenericService2Provider <string, int> service6 = serviceContainer.GetService <MyGenericService2Provider <string, int> >();

            Assert.IsNotNull(service6);

            IMyGenericListService <float> service7 = serviceContainer.GetService <IMyGenericListService <float> >();

            Assert.IsNull(service7);

            MyGenericService2Provider <string, string> service8 = serviceContainer.GetService <MyGenericService2Provider <string, string> >();

            Assert.IsNull(service8);

            IMySubService service9 = serviceContainer.GetService <IMySubService>();

            Assert.IsNull(service9);
        }
コード例 #2
0
        public void MyGenericServiceProvider2Wildcard()
        {
            var myProvider = new MyGenericService2Provider <string, int>();
            var myDict     = myProvider.GetService(typeof(IMyGenericDictionaryService <,>));

            Assert.IsNotNull(myDict);
            ((IMyGenericDictionaryService <string, int>)myDict).Set("k1", 9);
            ((IMyGenericDictionaryService <string, int>)myDict).Set("k2", 8);
            ((IMyGenericDictionaryService <string, int>)myDict).Set("k3", 7);
            Assert.AreEqual("k1=9;k2=8;k3=7;", myDict.ToString());
        }