本文整理汇总了C#中IServiceContainer.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# IServiceContainer.Contains方法的具体用法?C# IServiceContainer.Contains怎么用?C# IServiceContainer.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IServiceContainer
的用法示例。
在下文中一共展示了IServiceContainer.Contains方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VerifyOncePerRequest
private static bool VerifyOncePerRequest(string serviceName, IServiceContainer container)
{
// The container must be able to create an
// ISampleService instance
Assert.IsTrue(container.Contains(serviceName, typeof(ISampleService)), "Service not found!");
// Both instances must be unique
var first = container.GetService<ISampleService>(serviceName);
var second = container.GetService<ISampleService>(serviceName);
Assert.AreNotSame(first, second, "The two instances returned from the container must be unique!");
return true;
}
示例2: ResolveFrom
/// <summary>
/// Generates method arguments from the given <paramref name="parameterTypes"/>
/// and <paramref name="container"/>.
/// </summary>
/// <param name="parameterTypes">The parameter types for the target method.</param>
/// <param name="container">The container that will provide the method arguments.</param>
/// <param name="additionalArguments">The additional arguments that will be passed to the target method.</param>
/// <returns>An array of objects that represent the arguments to be passed to the target method.</returns>
public object[] ResolveFrom(IEnumerable<Type> parameterTypes, IServiceContainer container, params object[] additionalArguments)
{
var enumerableDefinition = typeof(IEnumerable<>);
var argumentList = new List<object>();
foreach (var parameterType in parameterTypes)
{
// Substitute the parameter if and only if
// the container does not have service that
// that matches the parameter type
var parameterTypeExists = container.Contains(parameterType);
if (parameterTypeExists)
{
// Instantiate the service type and build
// the argument list
object currentArgument = container.GetService(parameterType);
argumentList.Add(currentArgument);
continue;
}
// Determine if the parameter type is an IEnumerable<T> type
// and generate the list if necessary
if (parameterType.IsGenericType &&
parameterType.GetGenericTypeDefinition() == enumerableDefinition)
{
AddEnumerableArgument(parameterType, container, argumentList);
continue;
}
if (!parameterType.IsArray)
continue;
// Determine if the parameter type is an array
// of existing services and inject the current
// set of services as a parameter value
AddArrayArgument(parameterType, container, argumentList);
}
// Append the existing arguments
if (additionalArguments != null && additionalArguments.Length > 0)
argumentList.AddRange(additionalArguments);
return argumentList.ToArray();
}
示例3: VerifyOncePerThread
private static bool VerifyOncePerThread(string serviceName, IServiceContainer container)
{
var results = new List<ISampleService>();
Func<ISampleService> createService = () =>
{
var result = container.GetService<ISampleService>(serviceName);
lock (results)
{
results.Add(result);
}
return null;
};
Assert.IsTrue(container.Contains(serviceName, typeof(ISampleService)));
// Create the other instance from another thread
var asyncResult = createService.BeginInvoke(null, null);
// Two instances created within the same thread must be
// the same
var first = container.GetService<ISampleService>(serviceName);
var second = container.GetService<ISampleService>(serviceName);
Assert.IsNotNull(first);
Assert.AreSame(first, second);
// Wait for the other thread to finish executing
createService.EndInvoke(asyncResult);
Assert.IsTrue(results.Count > 0);
// The service instance created in the other thread
// must be unique
Assert.IsNotNull(results[0]);
Assert.AreNotSame(first, results[0]);
// NOTE: The return value will be ignored
return true;
}
示例4: Initialize
/// <summary>
/// Initializes the <see cref="ProxyFactory"/> instance
/// with the <paramref name="source"/> container.
/// </summary>
/// <param name="source">The <see cref="IServiceContainer"/> instance that will hold the ProxyFactory.</param>
public virtual void Initialize(IServiceContainer source)
{
if (source.Contains(typeof (IProxyBuilder), new Type[0]))
ProxyBuilder = (IProxyBuilder) source.GetService(typeof (IProxyBuilder));
if (source.Contains(typeof (IExtractInterfaces), new Type[0]))
InterfaceExtractor = (IExtractInterfaces) source.GetService(typeof (IExtractInterfaces));
//if (source.Contains(typeof(IVerifier)))
// Verifier = source.GetService<IVerifier>();
if (source.Contains(typeof (IProxyCache), new Type[0]))
Cache = (IProxyCache) source.GetService(typeof (IProxyCache));
}
示例5: ResolveFrom
/// <summary>
/// Generates method arguments from the given <paramref name="parameterTypes"/>
/// and <paramref name="container"/>.
/// </summary>
/// <param name="parameterTypes">The parameter types for the target method.</param>
/// <param name="container">The container that will provide the method arguments.</param>
/// <param name="additionalArguments">The additional arguments that will be passed to the target method.</param>
/// <returns>An array of objects that represent the arguments to be passed to the target method.</returns>
public object[] ResolveFrom(IEnumerable<INamedType> parameterTypes, IServiceContainer container,
params object[] additionalArguments)
{
Type enumerableDefinition = typeof (IEnumerable<>);
Type factoryDefinition = typeof (IFactory<>);
var argumentList = new List<object>();
foreach (INamedType namedType in parameterTypes)
{
Type parameterType = namedType.Type;
// Use the named service instance if possible
string parameterName = namedType.Name;
string serviceName = null;
if (!string.IsNullOrEmpty(parameterName) && parameterName.Length > 1)
{
string firstChar = parameterName.First().ToString();
string remainingText = parameterName.Substring(1);
serviceName = string.Format("{0}{1}", firstChar.ToUpper(), remainingText);
}
if (serviceName != null && container.Contains(serviceName, parameterType))
{
// Instantiate the service type and build
// the argument list
object currentArgument = container.GetService(serviceName, parameterType);
argumentList.Add(currentArgument);
continue;
}
// Substitute the parameter if and only if
// the container does not have service that
// that matches the parameter type
bool parameterTypeExists = container.Contains(parameterType);
if (parameterTypeExists)
{
// Instantiate the service type and build
// the argument list
object currentArgument = container.GetService(parameterType);
argumentList.Add(currentArgument);
continue;
}
// Determine if the parameter type is an IEnumerable<T> type
// and generate the list if necessary
if (parameterType.IsGenericType &&
parameterType.GetGenericTypeDefinition() == enumerableDefinition)
{
AddEnumerableArgument(parameterType, container, argumentList);
continue;
}
if (parameterType.IsArray)
{
// Determine if the parameter type is an array
// of existing services and inject the current
// set of services as a parameter value
AddArrayArgument(parameterType, container, argumentList);
continue;
}
}
// Append the existing arguments
if (additionalArguments != null && additionalArguments.Length > 0)
argumentList.AddRange(additionalArguments);
return argumentList.ToArray();
}
示例6: AddEnumerableArgument
/// <summary>
/// Determines whether or not a parameter type is an existing
/// list of available services and automatically constructs the
/// service list and adds it to the <paramref name="argumentList"/>.
/// </summary>
/// <param name="parameterType">The current constructor parameter type.</param>
/// <param name="container">The container that will provide the argument values.</param>
/// <param name="argumentList">The list that will hold the arguments to be passed to the constructor.</param>
private static void AddEnumerableArgument(Type parameterType, IServiceContainer container,
ICollection<object> argumentList)
{
Type elementType = parameterType.GetGenericArguments()[0];
Type baseElementDefinition = elementType.IsGenericType
? elementType.GetGenericTypeDefinition()
: null;
// There has to be at least one service
Func<IServiceInfo, bool> condition =
info => info.ServiceType == elementType;
// If the element is a generic type,
// we need to check for any available generic factory
// instances that might be able to create the element type
if (baseElementDefinition != null)
condition = condition.Or(info => info.ServiceType == baseElementDefinition);
if (!container.Contains(condition))
return;
var serviceList = new List<object>();
// Build the IEnumerable<> list of services
// that match the gvien condition
IEnumerable<IServiceInstance> services = container.GetServices(condition);
foreach (IServiceInstance service in services)
{
serviceList.Add(service.Object);
}
IEnumerable enumerable = serviceList.AsEnumerable();
object result = enumerable.Cast(elementType);
argumentList.Add(result);
}
示例7: VerifySingleton
private static bool VerifySingleton(string serviceName, IServiceContainer container)
{
// The container must be able to create the
// ISampleService instance
Assert.IsTrue(container.Contains(serviceName, typeof(ISampleService)));
// The container should return the singleton
var first = container.GetService<ISampleService>(serviceName);
var second = container.GetService<ISampleService>(serviceName);
Assert.AreSame(first, second);
return true;
}