本文整理汇总了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;
}