本文整理匯總了C#中SimpleInjector.Container.GetCurrentRegistrations方法的典型用法代碼示例。如果您正苦於以下問題:C# Container.GetCurrentRegistrations方法的具體用法?C# Container.GetCurrentRegistrations怎麽用?C# Container.GetCurrentRegistrations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類SimpleInjector.Container
的用法示例。
在下文中一共展示了Container.GetCurrentRegistrations方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: FindAssignableRegistrations
private static InstanceProducer[] FindAssignableRegistrations(Container container,
Type serviceType)
{
Type serviceTypeDefinition = serviceType.GetGenericTypeDefinition();
return (
from reg in container.GetCurrentRegistrations()
where reg.ServiceType.IsGenericType
where reg.ServiceType.GetGenericTypeDefinition() == serviceTypeDefinition
where serviceType.IsAssignableFrom(reg.ServiceType)
select reg)
.ToArray();
}
示例2: AllowToResolveVariantCollections
public static void AllowToResolveVariantCollections(Container container)
{
container.ResolveUnregisteredType += (sender, e) =>
{
// Only handle IEnumerable<T>.
if (!e.UnregisteredServiceType.IsGenericType ||
e.UnregisteredServiceType.GetGenericTypeDefinition() !=
typeof(IEnumerable<>))
{
return;
}
Type serviceType =
e.UnregisteredServiceType.GetGenericArguments()[0];
if (!serviceType.IsGenericType)
{
return;
}
Type def = serviceType.GetGenericTypeDefinition();
var registrations = (
from r in container.GetCurrentRegistrations()
where r.ServiceType.IsGenericType
where r.ServiceType.GetGenericTypeDefinition() == def
where serviceType.IsAssignableFrom(r.ServiceType)
select r)
.ToArray();
if (registrations.Any())
{
var instances =
registrations.Select(r => r.GetInstance());
var castMethod = typeof(Enumerable).GetMethod("Cast")
.MakeGenericMethod(serviceType);
var castedInstances =
castMethod.Invoke(null, new[] { instances });
e.Register(() => castedInstances);
}
};
}
示例3: GetTypes
private static IList<Type> GetTypes(Container container, Assembly assembly, Type[] typesToIgnore)
{
string assemblyName = assembly.GetName().Name;
var existingRegistrationsServiceTypes =
container.GetCurrentRegistrations().Select(instanceProducer => instanceProducer.ServiceType).ToList();
IList<Type> registrations =
assembly.GetExportedTypes()
.Where(type => !typesToIgnore.Contains(type))
.Where(type => !existingRegistrationsServiceTypes.Contains(type))
.Where(type => type.Namespace != null)
.Where(type => type.Namespace.StartsWith(assemblyName, StringComparison.OrdinalIgnoreCase))
.Where(type => (type.Attributes & TypeAttributes.Abstract) != TypeAttributes.Abstract)
.Where(type => type.GetInterfaces().Any())
.Where(
type =>
type.GetInterfaces()
.Any(
inter =>
!typesToIgnore.Contains(inter) && inter.Namespace != null &&
inter.Namespace.StartsWith(assemblyName, StringComparison.OrdinalIgnoreCase)))
.ToList();
// Ignore already registerd interfaces:
for (int i = registrations.Count() - 1; i >= 0; i--)
{
foreach (var registrationInterface in registrations[i].GetInterfaces())
{
if (existingRegistrationsServiceTypes.Contains(registrationInterface))
{
registrations.RemoveAt(i);
break;
}
}
}
return registrations;
}