本文整理汇总了C#中System.IocContainer.Resolve方法的典型用法代码示例。如果您正苦于以下问题:C# IocContainer.Resolve方法的具体用法?C# IocContainer.Resolve怎么用?C# IocContainer.Resolve使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IocContainer
的用法示例。
在下文中一共展示了IocContainer.Resolve方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RequestLifetimeReturnsSameInstanceForSameRequest
public void RequestLifetimeReturnsSameInstanceForSameRequest()
{
// Arrange
using (var container = new IocContainer())
{
var context1 = new FakeHttpContext("Http://fakeUrl1.com");
var context2 = new FakeHttpContext("Http://fakeUrl2.com");
HttpContextBase currentContext = null;
var lifetime = new RequestLifetime(() => currentContext); // better solution for injecting HttpContext ?
container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);
// Act
currentContext = context1;
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
currentContext = context2;
var result3 = container.Resolve<IFoo>();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2);
Assert.AreNotSame(result1, result3);
}
}
示例2: CachedLifetimeReturnsDifferentInstanceIfCacheExpired
public void CachedLifetimeReturnsDifferentInstanceIfCacheExpired()
{
using (var container = new IocContainer())
{
var policy = new CacheItemPolicy()
{
SlidingExpiration = new TimeSpan(0, 0, 1)
};
container.Register<IFoo>(c => new Foo1()).WithCachedLifetime(policy);
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
Thread.Sleep(1500);
var result3 = container.Resolve<IFoo>();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2);
Assert.AreNotSame(result1, result3);
}
}
示例3: RegisterInstanceCanRegisterAStruct
public void RegisterInstanceCanRegisterAStruct()
{
// Struct / ValueType ?
using (var container = new IocContainer())
{
int number = 32;
var reg1 = container.RegisterInstance(typeof(int), number);
var reg2 = container.RegisterInstance(number, "key");
// Check registrations
Assert.IsInstanceOfType(reg1, typeof(InstanceRegistration<int>));
Assert.IsInstanceOfType(reg2, typeof(InstanceRegistration<int>));
Assert.AreSame(reg1.ReturnType, typeof(int));
Assert.AreSame(reg2.ReturnType, typeof(int));
// Check index
Assert.IsTrue(container.Index.Contains(typeof(int)));
Assert.IsTrue(container.Index.Contains(typeof(int), "key"));
// Try to resolve
var result1 = (int)container.Resolve(typeof(int));
var result2 = container.Resolve<int>("key");
Assert.AreEqual(number, result1);
Assert.AreEqual(number, result2);
Assert.AreEqual(result1, result2);
}
}
示例4: CachedLifetimeReturnsDifferentInstanceIfSlidingTimeoutHaveExpired
public void CachedLifetimeReturnsDifferentInstanceIfSlidingTimeoutHaveExpired()
{
using (var container = new IocContainer())
{
// Expires When not Accessed For More than a specific time periode.
var lifetime = new CachedLifetime(new TimeSpan(0, 0, 1));
container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
// Simulate expiry
Thread.Sleep(2000);
var result3 = container.Resolve<IFoo>();
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2);
Assert.AreNotSame(result1, result3);
}
}
示例5: CachedLifetimeIsDependentOnTest
public void CachedLifetimeIsDependentOnTest()
{
using (var container = new IocContainer())
{
var executionDirectory = Environment.CurrentDirectory;
// Create a file for the cached item to be dependent on
var filePath = executionDirectory + "\\DependencyFile.txt";
if (File.Exists(filePath))
File.Delete(filePath);
var dependencyFile = File.CreateText(filePath);
dependencyFile.WriteLine("This is a file that the cache item is dependent on.");
dependencyFile.Close();
var cacheDependency = new CacheDependency(filePath);
var lifetime = new CachedLifetime(cacheDependency);
container.Register<IFoo>(c => new Foo1()).SetLifetime(lifetime);
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
// Change the dependency file
dependencyFile = File.AppendText(filePath);
dependencyFile.WriteLine("Modified dependecy file.");
dependencyFile.Close();
// Need to give the system time to detect the change.
Thread.Sleep(500);
var result3 = container.Resolve<IFoo>();
// cleanup
if (File.Exists(filePath))
File.Delete(filePath);
// Assert
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsNotNull(result3);
Assert.AreSame(result1, result2);
Assert.AreNotSame(result1, result3);
}
}
示例6: CanChangeLifetimeUsingSetLifetime
public void CanChangeLifetimeUsingSetLifetime()
{
using (var container = new IocContainer(() => new TransientLifetime()))
{
Assert.IsInstanceOfType(container.DefaultLifetimeFactory(), typeof(TransientLifetime));
var registration = container.Register<IFoo>(c => new Foo1()).SetLifetime(new ContainerLifetime());
Assert.IsInstanceOfType(registration.Lifetime, typeof(ContainerLifetime));
var foo1 = container.Resolve<IFoo>();
var foo2 = container.Resolve<IFoo>();
Assert.AreSame(foo1, foo2);
}
}
示例7: CachedLifetimeManagerExtensionReturnsSameObjectIfCacheNotExpired
public void CachedLifetimeManagerExtensionReturnsSameObjectIfCacheNotExpired()
{
using (var iocContainer = new IocContainer())
{
iocContainer.Register<IFoo>(c => new Foo1()).AsCached();
var result1 = iocContainer.Resolve<IFoo>();
var result2 = iocContainer.Resolve<IFoo>();
var result3 = iocContainer.Resolve<IFoo>();
Verify.That(result1).IsNotNull()
.IsTheSameObjectAs(result2)
.IsTheSameObjectAs(result3);
}
}
示例8: CanRegisterAndResolveMultipleTypes
public void CanRegisterAndResolveMultipleTypes()
{
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1());
container.Register<IBar>(c => new Bar1());
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IBar>();
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsInstanceOfType(result1, typeof(Foo1));
Assert.IsInstanceOfType(result2, typeof(Bar1));
}
}
示例9: MultipleResolvesReturnDifferentInstances
public void MultipleResolvesReturnDifferentInstances()
{
// Belongs to the lifetime?
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1());
var result1 = container.Resolve<IFoo>();
var result2 = container.Resolve<IFoo>();
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.AreNotSame(result1, result2);
}
}
示例10: OnNavigatedTo
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var ioc = new IocContainer();
CustomRegistrations(ioc);
AutoRegistrations(ioc);
var messages = new List<string>
{
ioc.Resolve<ISomething>().Say(),
ioc.Resolve<Something>().Say(),
ioc.Resolve<ISomethingOrOther>().Say(),
ioc.Resolve<IInterfaceForBaseType>().Say(),
ioc.Resolve<IInterfaceForDerivedType>().Say(),
ioc.Resolve<ICustomInterface>().Say()
};
try
{
ioc.Resolve<Object>();
}
catch (TypeNotRegisteredException ex)
{
messages.Add(ex.Message);
}
var dialogContent = string.Empty;
messages.ForEach(m => dialogContent += m + Environment.NewLine);
await new MessageDialog(dialogContent).ShowAsync();
Application.Current.Exit();
}
示例11: AttemptingToResolveANonexistingEntryThrows
public void AttemptingToResolveANonexistingEntryThrows()
{
using (var iocContainer = new IocContainer())
{
Verify.TheExpectedException(typeof(KeyNotFoundException))
.IsThrownWhen(() => iocContainer.Resolve<IFoo>("Bob"))
.AndHasAMessageThat().IsEqualTo("Munq IocContainer failed to resolve Munq.Test.IFoo");
}
}
示例12: RegistrationsWithDifferentNameResolveToDifferentTypes
public void RegistrationsWithDifferentNameResolveToDifferentTypes()
{
using (var container = new IocContainer())
{
container.Register<IFoo>(c => new Foo1(), "Foo1");
container.Register<IFoo>(c => new Foo2(), "Foo2");
var result1 = container.Resolve<IFoo>("Foo1");
var result2 = container.Resolve<IFoo>("Foo2");
Assert.IsNotNull(result1);
Assert.IsNotNull(result2);
Assert.IsInstanceOfType(result1, typeof(Foo1));
Assert.IsInstanceOfType(result2, typeof(Foo2));
Assert.AreNotSame(result1, result2);
}
}
示例13: Example1
public Example1()
{
IIocContainer container = new IocContainer();
container.Register<IMyExample1, MyExample1>();
IMyExample1 temp = container.Resolve<IMyExample1>();
Console.WriteLine("Example 1 : {0}" ,temp.Text);
}
示例14: Example3
public Example3()
{
IIocContainer container = new IocContainer();
container.RegisterUnResolvedTypes = true;
IMyExample3 temp = container.Resolve<MyExample3>();
Console.WriteLine("Example 3 : {0}" ,temp.Text);
}
示例15: CompileCanInlineRegistrationsWithContainerLifetime
public void CompileCanInlineRegistrationsWithContainerLifetime()
{
// Doesnt check that Compile actually works - just that it doesnt break anything
using (var container = new IocContainer())
{
container.Register<IFoo>(x => new Foo1()).ContainerLifetime();
container.Register<IBar>(x => new Bar1());
var reg = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>(), x.Resolve<IBar>()));
container.Compile();
var result1 = container.Resolve<IFoo>();
var result2 = (IFooBar)container.Resolve(reg);
Assert.AreEqual(result1, result2.Foo);
}
}