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


C# UnityContainer.Resolve方法代码示例

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


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

示例1: ContainerControlledLifetimeReturnsSameInstanceFromMultipleThreads

        public void ContainerControlledLifetimeReturnsSameInstanceFromMultipleThreads()
        {
            IUnityContainer container = new UnityContainer()
                .AddExtension(new SpyExtension(new DelayStrategy(), UnityBuildStage.Lifetime))
                .RegisterType<object>(new ContainerControlledLifetimeManager());

            object result1 = null;
            object result2 = null;

            Thread thread1 = new Thread(delegate()
            {
                result1 = container.Resolve<object>();
            });

            Thread thread2 = new Thread(delegate()
            {
                result2 = container.Resolve<object>();
            });

            thread1.Start();
            thread2.Start();

            thread2.Join();
            thread1.Join();

            Assert.IsNotNull(result1);
            Assert.AreSame(result1, result2);
        }
开发者ID:kangkot,项目名称:unity,代码行数:28,代码来源:ContainerControlledLifetimeThreadingFixture.cs

示例2: Main

 static void Main(string[] args)
 {
     var container = new UnityContainer();
       container.RegisterType<IFoo,Foo>(new ContainerControlledLifetimeManager(), new InjectionConstructor("Scott"));
       IFoo myFoo = container.Resolve<IFoo>();
       IFoo myFoo2 = container.Resolve<IFoo>();
       Console.WriteLine(myFoo.Name);
       Console.WriteLine(Object.ReferenceEquals(myFoo, myFoo2));
 }
开发者ID:sblom,项目名称:sandbox,代码行数:9,代码来源:Program.cs

示例3: DifferentResolveCallsReturnDifferentLazyInstances

        public void DifferentResolveCallsReturnDifferentLazyInstances()
        {
            var container = new UnityContainer()
                .RegisterType<ILogger, MockLogger>();

            var lazy1 = container.Resolve<Lazy<ILogger>>();
            var lazy2 = container.Resolve<Lazy<ILogger>>();

            Assert.AreNotSame(lazy1.Value, lazy2.Value);
        }
开发者ID:kangkot,项目名称:unity,代码行数:10,代码来源:LazyResolveFixture.cs

示例4: CanRegisterLifetimeManagerInContainerAndUseItOnOneThread

        public void CanRegisterLifetimeManagerInContainerAndUseItOnOneThread()
        {
            IUnityContainer container = new UnityContainer()
                .RegisterType<object>(new PerThreadLifetimeManager());

            object result1 = container.Resolve<object>();
            object result2 = container.Resolve<object>();

            Assert.AreSame(result1, result2);
        }
开发者ID:theoju,项目名称:CleanCode,代码行数:10,代码来源:PerThreadLifetimeManagerFixture.cs

示例5: CanResolveTypeRegisteredWithAllInterfaces

        public void CanResolveTypeRegisteredWithAllInterfaces()
        {
            var container = new UnityContainer();
            container.RegisterTypes(AllClasses.FromAssemblies(typeof(MockLogger).GetTypeInfo().Assembly).Where(t => t == typeof(MockLogger)), WithMappings.FromAllInterfaces, WithName.Default, WithLifetime.ContainerControlled);

            var logger1 = container.Resolve<ILogger>();
            var logger2 = container.Resolve<ILogger>();

            Assert.IsInstanceOfType(logger1, typeof(MockLogger));
            Assert.AreSame(logger1, logger2);
        }
开发者ID:kangkot,项目名称:unity,代码行数:11,代码来源:RegistrationByConventionFixture.cs

示例6: ViewsAreDifferentInDifferentResolveCalls

        public void ViewsAreDifferentInDifferentResolveCalls()
        {
            var container = new UnityContainer()
                .RegisterType<IPresenter, MockPresenter>()
                .RegisterType<IView, View>(new PerResolveLifetimeManager());

            var view1 = container.Resolve<IView>();
            var view2 = container.Resolve<IView>();

            Assert.AreNotSame(view1, view2);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:11,代码来源:PerResolveLifetimeFixture.cs

示例7: DifferentLazyGenericsGetTheirOwnBuildPlan

        public void DifferentLazyGenericsGetTheirOwnBuildPlan()
        {
            var container = new UnityContainer()
                .RegisterType<ILogger, MockLogger>()
                .RegisterInstance<string>("the instance");

            var lazy1 = container.Resolve<Lazy<ILogger>>();
            var lazy2 = container.Resolve<Lazy<string>>();

            AssertExtensions.IsInstanceOfType(lazy1.Value, typeof(ILogger));
            Assert.AreEqual("the instance", lazy2.Value);
        }
开发者ID:kangkot,项目名称:unity,代码行数:12,代码来源:LazyResolveFixture.cs

示例8: CanOverrideExistingMappingWithMappingForDifferentName

        public void CanOverrideExistingMappingWithMappingForDifferentName()
        {
            var container = new UnityContainer();
            container.RegisterType<object, string>("string");
            container.RegisterInstance("string", "a string");
            container.RegisterInstance("int", 42);

            container.RegisterTypes(new[] { typeof(int) }, t => new[] { typeof(object) }, t => "int");

            Assert.AreEqual("a string", container.Resolve<object>("string"));
            Assert.AreEqual(42, container.Resolve<object>("int"));
        }
开发者ID:kangkot,项目名称:unity,代码行数:12,代码来源:RegistrationByConventionFixture.cs

示例9: OverrideDoesntLastAfterResolveCall

        public void OverrideDoesntLastAfterResolveCall()
        {
            const int ConfiguredValue = 15; // Just need a number, value has no signficance.
            const int OverrideValue = 42; // Just need a number, value has no significance.
            var container = new UnityContainer()
                .RegisterType<SimpleTestObject>(new InjectionConstructor(ConfiguredValue));

            container.Resolve<SimpleTestObject>(new ParameterOverride("x", OverrideValue).OnType<SimpleTestObject>());

            var result = container.Resolve<SimpleTestObject>();

            Assert.AreEqual(ConfiguredValue, result.X);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:13,代码来源:ServiceOverrideFixture.cs

示例10: DependenciesAndInterceptionMixProperly

        public void DependenciesAndInterceptionMixProperly()
        {
            var container = new UnityContainer()
                .AddNewExtension<Interception>()
                .RegisterType<IRepository, TestRepository>()
                .RegisterType<TestService>(
                    new Interceptor<VirtualMethodInterceptor>());

            var svc1 = container.Resolve<TestService>();
            var svc2 = container.Resolve<TestService>();

            Assert.AreNotSame(svc1, svc2);
            Assert.IsNotNull(svc1 as IInterceptingProxy);
            Assert.IsNotNull(svc2 as IInterceptingProxy);
        }
开发者ID:kangkot,项目名称:unity,代码行数:15,代码来源:CodeplexIssuesFixture.cs

示例11: StaticMethodsShouldNotBeInjected

        public void StaticMethodsShouldNotBeInjected()
        {
            IUnityContainer container = new UnityContainer();

            GuineaPig pig = container.Resolve<GuineaPig>();
            Assert.IsFalse(GuineaPig.StaticMethodWasCalled);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:7,代码来源:MethodInjectionFixture.cs

示例12: CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers

        public void CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers()
        {
            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();

            container
                .Configure<Interception>()
                    .AddPolicy("policy1")
                        .AddMatchingRule("rule1")
                        .AddCallHandler("handler1")
                        .AddCallHandler("handler2").Interception.Container
                .RegisterType<IMatchingRule, AlwaysMatchingRule>("rule1")
                .RegisterType<ICallHandler, GlobalCountCallHandler>(
                    "handler1",
                    new InjectionConstructor("handler1"))
                .RegisterType<ICallHandler, GlobalCountCallHandler>(
                    "handler2",
                    new InjectionConstructor("handler2"),
                    new InjectionProperty("Order", 10));

            GlobalCountCallHandler.Calls.Clear();

            container
                .Configure<Interception>()
                    .SetInterceptorFor<Wrappable>("wrappable", new VirtualMethodInterceptor());

            Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable");
            wrappable1.Method2();

            Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]);
            Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]);
        }
开发者ID:theoju,项目名称:CleanCode,代码行数:32,代码来源:ConvenienceConfigurationFixture.cs

示例13: OptionalDependencyParametersAreInjectedWithNull

        public void OptionalDependencyParametersAreInjectedWithNull()
        {
            IUnityContainer container = new UnityContainer();

            var result = container.Resolve<ObjectWithOptionalConstructorParameter>();
            Assert.IsNull(result.SomeInterface);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:7,代码来源:OptionalDependencyAttributeFixture.cs

示例14: CanResolveALazy

        public void CanResolveALazy()
        {
            var container = new UnityContainer()
                .RegisterType<ILogger, MockLogger>();

            var lazy = container.Resolve<Lazy<ILogger>>();
            Assert.IsNotNull(lazy);
        }
开发者ID:kangkot,项目名称:unity,代码行数:8,代码来源:LazyResolveFixture.cs

示例15: OptionalPropertiesGetNullWhenNotConfigured

        public void OptionalPropertiesGetNullWhenNotConfigured()
        {
            IUnityContainer container = new UnityContainer();

            var result = container.Resolve<ObjectWithOptionalProperty>();

            Assert.IsNull(result.SomeInterface);
        }
开发者ID:jorgeds001,项目名称:CodeSamples,代码行数:8,代码来源:OptionalDependencyAttributeFixture.cs


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