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


C# Container.GetService方法代码示例

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


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

示例1: AsAndAsUseTest

        public void AsAndAsUseTest()
        {
            using (var ioc = new Container())
            {
                ioc.As<ITestDisposable>().AndAs<IDemoInterface>().Use<MockDisposableClass>(ThreadScope.Instance);
                var instance1 = ioc.GetService<ITestDisposable>();
                var someInstance = ioc.GetService<IDemoInterface>();
                
                Assert.AreSame(instance1, someInstance);

            }
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:12,代码来源:AccordContainerTest.cs

示例2: SimpleEventTest

        public void SimpleEventTest()
        {
            using (var ioc = new Container())
            {
                IConfiguredFactory<MockDisposableClass> configuredFactory
                    = ioc.As<ITestDisposable>().Use<MockDisposableClass>(ThreadScope.Instance);

                var created = false;
                configuredFactory.AfterCreate += (sender, args) =>
                                                     {
                                                         Console.Write("Instance created!");
                                                         created = true;
                                                     };
                
                var instance1 = ioc.GetService<ITestDisposable>();
                Assert.IsTrue(created);

            }

        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:20,代码来源:AccordContainerTest.cs

示例3: DisposeRegistration

        public void DisposeRegistration()
        {
            var disposed = false;
            var created = false;
            using (var ioc = new Container())
             {
                 IConfiguredFactory<MockDisposableClass> registration = ioc.As<ITestDisposable>().Use<MockDisposableClass>(ioc);
                 registration.AfterCreate += (sender, args) =>
                                                       {
                                                           
                                                           args.Component.BeforeDisposeAction =() => disposed = true;
                                                           //((AfterCreateEventArgs)args).Component = new MockDisposableClass2();
                                                       };
                 var myComponent = ioc.GetService<ITestDisposable>();
                 registration.Dispose();

                 Assert.IsTrue(disposed);
                 Assert.IsFalse(ioc.IsConfiguredFor(typeof(ITestDisposable)));
             }
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:20,代码来源:AccordContainerTest.cs

示例4: RegisterInstanceNotExternallyOwnedTest

 public void RegisterInstanceNotExternallyOwnedTest() //todo: tests with opened generics types
 {
     var anyInstance = new MockDisposableClass();
     using (var ioc = new Container())
     {
         ioc.As<ITestDisposable>().Use(anyInstance, externallyOwned: false);
         ioc.GetService<ITestDisposable>();//creating isntance
     }
     Assert.AreEqual(true, anyInstance.Disposed);//instance is not externally owned -> dispose has been called
 }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:10,代码来源:AccordContainerTest.cs

示例5: RegisterInstanceScopedTest

        [Ignore] //This feature is not implemented yet.
        public void RegisterInstanceScopedTest() //todo: tests with opened generics types
        {
            var anyInstance = new MockDisposableClass();
            using (var ioc = new Container())
            {
                ioc.As<ITestDisposable>().Use(anyInstance,true,ThreadScope.Instance); //just for current thread
                Task.Factory.StartNew(() =>
                                          {
                                              var myInstance = ioc.GetService<ITestDisposable>();
                                              Assert.IsNull(myInstance); //should be null, because this is another thread
                                          }).Wait();
            }

        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:15,代码来源:AccordContainerTest.cs

示例6: RegisterInstanceTest

 public void RegisterInstanceTest() //todo: tests with opened generics types
 {
     var anyInstance = new MockDisposableClass();
     using (var ioc = new Container())
     {
         ioc.As<ITestDisposable>().Use(anyInstance);
         var myInstance = ioc.GetService<ITestDisposable>(); //creating isntance
         Assert.AreSame(anyInstance,myInstance); //should be some instance
     }
     
 }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:11,代码来源:AccordContainerTest.cs

示例7: MultiThreadTest

        public void MultiThreadTest()
        {
            using (var ioc = new Container())
            {
                ioc.As<LongTimeConstructedClass>().Use<LongTimeConstructedClass>(ioc);
                List<Task> all = new List<Task>();
                for (int n = 0;n<100;n++)
                {
                    Task t = new Task(()=>ioc.GetService<LongTimeConstructedClass>());
                    all.Add(t);
                    t.Start();
                }
                all.ForEach(t=>t.Wait());

            }
            Assert.AreEqual(1,LongTimeConstructedClass.Counter);
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:17,代码来源:AccordContainerTest.cs

示例8: Advanced1bTest

         public virtual void Advanced1bTest()
         {
             using (var ioc = new Container())
             {
                 //tvorba listu vyzaduje v konstruktoru číslo (kapacita listu)
                 ioc.As(typeof (int)).Use(() => 5);

                

                 ioc.As(typeof (IList<>)).Use(typeof (List<>),ioc/*singleton*/);
                 
                 Assert.AreEqual(5, ioc.GetService<int>()); //jen pro kontrolu

                 var f1 = (Func<IList<string>>) ioc.GetService(typeof (Func<IList<string>>));
                 var f2 = (Func<IList<string>>) ioc.GetService(typeof (Func<IList<string>>));

                 //protoze IList je definovan jako singleton, tak list vraceny pomocí teto fce musí byt vzdy stejný
                 Assert.AreSame(f1(), f2());
                 //a dokonce by to mela byt i stejná fce
                 Assert.AreSame(f1, f2);

                 var l1 = ioc.GetService<Lazy<IList<string>>>();
                 var l2 = ioc.GetService<Lazy<IList<string>>>();

                 //protoze IList je definovan jako singleton, tak list vraceny pomocí teto fce musí byt vzdy stejný
                 Assert.AreSame(l1.Value, l2.Value);
                 //a dokonce by to mela byt i stejné instance
                 Assert.AreSame(l1, l2);

                 var list = ioc.GetService<IList<string>>();
                 //jednoduse vraceny list musí byt stejný jako vraceny pomocí Lazy
                 Assert.AreSame(list, l1.Value);

                 //jednoduse vraceny list musí byt stejný jako list vraceny pomoci funkce 
                 Assert.AreSame(list, f1());
             }
         }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:37,代码来源:AccordContainerTest.cs

示例9: EventTest

        public void  EventTest()
        {
            byte released = 0;
            byte created = 0;
            using (var ioc = new Container())
            {
                //todo: zavest IConfiguredPluginGroup<TCommonType>
                var expr = ioc.As<ITestDisposable>().AndAs<IDemoInterface>().AndAs<IDisposable>();
                IConfiguredFactory<MockDisposableClass> regInfo = expr.Use<MockDisposableClass>(ioc
                    /*=singleton*/);

                regInfo.AfterCreate += (sender, args) =>
                                           {
                                               created++; //must be called only once!
                                           };
                regInfo.BeforeRelease += (sender, args) =>
                                             {
                                                 released++; //must be called only once!
                                                 
                                                 args.RunDispose = true; //it is by default, only for demonstration.
                                             };

                var myComponent1 = ioc.GetService<IDemoInterface>();
                var myComponent2 = ioc.GetService<ITestDisposable>();
                var myComponent3 = ioc.GetService<IDisposable>();
                
                Assert.AreSame(myComponent1, myComponent2);
                Assert.AreSame(myComponent1, myComponent3);
                Assert.IsTrue(created == 1); //was called only once!
            }
            Assert.IsTrue(released == 1);//was called only once!
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:32,代码来源:AccordContainerTest.cs

示例10: Dispose1Test

         public virtual void Dispose1Test()
         {
             bool disposed = false;
             ITestDisposable a;
             using (var ioc = new Container())
             {
                 var registration = ioc.As<ITestDisposable>().Use<MockDisposableClass>(ioc); //as singleton /* AccordConatiner implements IPersunScope*/
                 

                 a = ioc.GetService<ITestDisposable>();
                 a.BeforeDisposeAction =
                     delegate
                     {
                         disposed = true;
                     };

             }
             Assert.IsTrue(disposed);
         }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:19,代码来源:AccordContainerTest.cs

示例11: DisposeServiceTest

        public virtual void DisposeServiceTest()
        {
            bool disposed = false;
            using (var ioc = new Container())
            {
                ioc.As<ITestDisposable>().Use<MockDisposableClass>(ioc); //as singleton /* AccordConatiner implements IPersunScope*/
                var a = ioc.GetService<ITestDisposable>();
                a.BeforeDisposeAction = 
                    delegate 
                    { 
                        disposed = true; 
                    };

                Assert.IsTrue(ioc.IsConfiguredFor(typeof(ITestDisposable)));

                ioc.DisposeService(typeof(ITestDisposable));

                Assert.IsTrue(disposed);
                Assert.IsFalse(ioc.IsConfiguredFor(typeof(ITestDisposable)));
            }
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:21,代码来源:AccordContainerTest.cs

示例12: OpenGeneric_Shared_Test

        public void OpenGeneric_Shared_Test()
        {

            using (var ioc = new Container())
            {
                ioc.ConfigurationManager.SupportForIEnumerable = false;//protoze predefinováváme IEnumerable<>
                
                ioc.As<int>().Use(() => 2); //list potrebuje pocet polozek
                ioc.As(typeof(IList<>)).AndAs(typeof(IEnumerable<>)).Use(typeof(List<>),ioc/*=singleton*/);
                var a = ioc.GetService<IList<string>>();
                Assert.IsNotNull(a);
                var b = ioc.GetService<IEnumerable<string>>();
                Assert.AreSame(a,b);
                
            }
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:16,代码来源:AccordContainerTest.cs

示例13: OpenGeneric_Events_Test

        public void OpenGeneric_Events_Test()
        {
            bool released = false;
            using (var ioc = new Container())
            {
                bool created = false;

                IConfiguredFactory regInfo =
                    ioc.As(typeof (IList<>)).Use(typeof (List<>).GetConstructor(Type.EmptyTypes), ioc /*=singleton*/);
                regInfo.AfterCreate += (sender, args) =>
                                           {
                                               created = true;
                                              
                                           };
                regInfo.BeforeRelease += (sender, args) =>
                                             {
                                                 released = true;
                                             };
                
                var list = ioc.GetService<IList<string>>();
                Assert.AreEqual(true, created);
                Assert.AreEqual(false, released);
            }
            Assert.AreEqual(true, released);
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:25,代码来源:AccordContainerTest.cs

示例14: OpenGeneric_Simple_Test

 public void OpenGeneric_Simple_Test()
 {
     
     using (var ioc = new Container())
     {
         
         IConfiguredFactory regInfo = ioc.As(typeof(IList<>)).Use(typeof(List<>).GetConstructor(Type.EmptyTypes));
         var list = ioc.GetService<IList<string>>();
         Assert.IsNotNull(list);
         
     }
 }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:12,代码来源:AccordContainerTest.cs

示例15: UsingConstructorForOpenGenericTest

        public void UsingConstructorForOpenGenericTest()
        {
            using (var ioc = new Container())
            {

                ioc.As(typeof(IList<>)).Use(typeof(List<>).GetConstructor(new Type[] {}));//we are using empty constructor for List<>
                var list = ioc.GetService<IList<string>>();
                Assert.IsNotNull(list);
            }
        }
开发者ID:vladka,项目名称:Fim.Accord,代码行数:10,代码来源:AccordContainerTest.cs


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