本文整理汇总了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);
}
示例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));
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
示例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"));
}
示例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);
}
示例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);
}
示例11: StaticMethodsShouldNotBeInjected
public void StaticMethodsShouldNotBeInjected()
{
IUnityContainer container = new UnityContainer();
GuineaPig pig = container.Resolve<GuineaPig>();
Assert.IsFalse(GuineaPig.StaticMethodWasCalled);
}
示例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"]);
}
示例13: OptionalDependencyParametersAreInjectedWithNull
public void OptionalDependencyParametersAreInjectedWithNull()
{
IUnityContainer container = new UnityContainer();
var result = container.Resolve<ObjectWithOptionalConstructorParameter>();
Assert.IsNull(result.SomeInterface);
}
示例14: CanResolveALazy
public void CanResolveALazy()
{
var container = new UnityContainer()
.RegisterType<ILogger, MockLogger>();
var lazy = container.Resolve<Lazy<ILogger>>();
Assert.IsNotNull(lazy);
}
示例15: OptionalPropertiesGetNullWhenNotConfigured
public void OptionalPropertiesGetNullWhenNotConfigured()
{
IUnityContainer container = new UnityContainer();
var result = container.Resolve<ObjectWithOptionalProperty>();
Assert.IsNull(result.SomeInterface);
}