本文整理汇总了C#中LinFu.IoC.ServiceContainer.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceContainer.Contains方法的具体用法?C# ServiceContainer.Contains怎么用?C# ServiceContainer.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类LinFu.IoC.ServiceContainer
的用法示例。
在下文中一共展示了ServiceContainer.Contains方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldLoadAssemblyIntoLoaderAtRuntime
public void ShouldLoadAssemblyIntoLoaderAtRuntime()
{
string path = Path.Combine(@"..\..\..\SampleFileWatcherLibrary\bin\Debug",
AppDomain.CurrentDomain.BaseDirectory);
string targetFile = "SampleFileWatcherLibrary.dll";
string sourceFileName = Path.Combine(path, targetFile);
var container = new ServiceContainer();
container.AutoLoadFrom(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
// There should be nothing loaded at this point since the assembly hasn't
// been copied to the target directory yet
Assert.IsFalse(container.Contains(typeof (ISampleService)));
// Copy the assembly to the target directory
// and watch for changes
string targetFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dummy.dll");
File.Copy(sourceFileName, targetFileName, true);
// Give the watcher thread enough time to load the assembly into memory
Thread.Sleep(500);
Assert.IsTrue(container.Contains(typeof (ISampleService)));
var instance = container.GetService<ISampleService>();
Assert.IsNotNull(instance);
string typeName = instance.GetType().Name;
Assert.AreEqual("SampleFileWatcherServiceClassAddedAtRuntime", typeName);
}
示例2: ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions
public void ContainerMustAllowInjectingCustomFactoriesForOpenGenericTypeDefinitions()
{
var container = new ServiceContainer();
var factory = new SampleOpenGenericFactory();
container.AddFactory(typeof (ISampleGenericService<>), factory);
// The container must report that it *can* create
// the generic service type
Assert.IsTrue(container.Contains(typeof (ISampleGenericService<int>)));
var result = container.GetService<ISampleGenericService<int>>();
Assert.IsNotNull(result);
Assert.IsTrue(result.GetType() == typeof (SampleGenericImplementation<int>));
}
示例3: LazyObjectShouldNeverBeInitialized
public void LazyObjectShouldNeverBeInitialized()
{
var container = new ServiceContainer();
container.AddService<IProxyFactory>(new ProxyFactory());
container.AddService<IMethodBuilder<MethodInfo>>(new MethodBuilder());
Assert.IsTrue(container.Contains(typeof (IProxyFactory)));
var proxyFactory = container.GetService<IProxyFactory>();
var interceptor = new LazyInterceptor<ISampleService>(() => new SampleLazyService());
SampleLazyService.Reset();
// The instance should be uninitialized at this point
var proxy = proxyFactory.CreateProxy<ISampleService>(interceptor);
Assert.IsFalse(SampleLazyService.IsInitialized);
// The instance should be initialized once the method is called
proxy.DoSomething();
Assert.IsTrue(SampleLazyService.IsInitialized);
}
示例4: ContainerServicesShouldBeLazyIfProxyFactoryExists
public void ContainerServicesShouldBeLazyIfProxyFactoryExists()
{
var container = new ServiceContainer();
container.AddService<IProxyFactory>(new ProxyFactory());
Assert.IsTrue(container.Contains(typeof (IProxyFactory)));
// The instance should never be created
container.AddService(typeof (ISampleService), typeof (SampleLazyService));
var result = container.GetService<ISampleService>();
Assert.IsFalse(SampleLazyService.IsInitialized);
}
示例5: ContainerMustUseUnnamedContainsMethodIfNameIsNull
public void ContainerMustUseUnnamedContainsMethodIfNameIsNull()
{
var mockFactory = new Mock<IFactory>();
var mockService = new Mock<ISerializable>();
var container = new ServiceContainer();
Type serviceType = typeof (ISerializable);
// Use unnamed AddFactory method
container.AddFactory(serviceType, mockFactory.Object);
// The container should use the
// IContainer.Contains(Type) method instead of the
// IContainer.Contains(string, Type) method if the
// service name is blank
Assert.IsTrue(container.Contains(null, typeof (ISerializable)));
}
示例6: ContainerMustLoadSpecificGenericServiceTypesFromAGenericConcreteClassMarkedWithTheImplementsAttribute
public void ContainerMustLoadSpecificGenericServiceTypesFromAGenericConcreteClassMarkedWithTheImplementsAttribute()
{
var container = new ServiceContainer();
container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
string serviceName = "SpecificGenericService";
// The container must be able to create both registered service types
Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<int>)));
Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<double>)));
// Both service types must be valid services
Assert.IsNotNull(container.GetService<ISampleGenericService<int>>());
Assert.IsNotNull(container.GetService<ISampleGenericService<double>>());
}
示例7: ContainerMustLoadAssemblyFromMemory
public void ContainerMustLoadAssemblyFromMemory()
{
var container = new ServiceContainer();
container.LoadFrom(typeof (SampleClass).Assembly);
// Verify that the container loaded the sample assembly into memory
Assert.IsTrue(container.Contains(typeof (ISampleService)));
}
示例8: ServiceContainer
public void ContainerMustLoadAnyGenericServiceTypeInstanceFromAGenericConcreteClassMarkedWithTheImplementsAttribute()
{
var container = new ServiceContainer();
container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");
string serviceName = "NonSpecificGenericService";
// The container must be able to create any type that derives from ISampleService<T>
// despite whether or not the specific generic service type is explicitly registered as a service
Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<int>)));
Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<double>)));
Assert.IsTrue(container.Contains(serviceName, typeof (ISampleGenericService<string>)));
// Both service types must be valid services
Assert.IsNotNull(container.GetService<ISampleGenericService<int>>());
Assert.IsNotNull(container.GetService<ISampleGenericService<double>>());
Assert.IsNotNull(container.GetService<ISampleGenericService<string>>());
}
示例9: ContainerMustHoldNamedFactoryInstance
public void ContainerMustHoldNamedFactoryInstance()
{
var mockFactory = new Mock<IFactory>();
var container = new ServiceContainer();
// Randomly assign an interface type
// NOTE: The actual interface type doesn't matter
Type serviceType = typeof (ISerializable);
container.AddFactory("MyService", serviceType, mockFactory.Object);
Assert.IsTrue(container.Contains("MyService", serviceType),
"The container is supposed to contain a service named 'MyService'");
var instance = new object();
mockFactory.Expect(f => f.CreateInstance(
It.Is<IFactoryRequest>(
request => request.ServiceName == "MyService" && request.ServiceType == serviceType)))
.Returns(instance);
Assert.AreSame(instance, container.GetService("MyService", serviceType));
}
示例10: ContainerMustHoldAnonymousFactoryInstance
public void ContainerMustHoldAnonymousFactoryInstance()
{
var mockFactory = new Mock<IFactory>();
var container = new ServiceContainer();
// Give it a random service interface type
Type serviceType = typeof (IDisposable);
// Manually add the factory instance
container.AddFactory(serviceType, mockFactory.Object);
Assert.IsTrue(container.Contains(serviceType),
"The container needs to have a factory for service type '{0}'", serviceType);
}
示例11: ShouldReportThatServiceExistsForStronglyTypedFunctor
public void ShouldReportThatServiceExistsForStronglyTypedFunctor()
{
var container = new ServiceContainer();
Func<int, int, int> addOperation1 = (a, b) => a + b;
container.AddService("Add", addOperation1);
Assert.IsTrue(container.Contains("Add", typeof (int), 1, 1));
}