Esempio n. 1
0
 public ICRegILGen ResolveNeedBy(Type type, object key)
 {
     ICReg registration;
     if (_container.Registrations.TryGetValue(new KeyAndType(key, type), out registration))
     {
         var multi = registration as ICRegMulti;
         if (multi != null)
         {
             registration = ChooseFromMulti(multi, false) ?? multi.ChosenOne;
         }
     }
     if (registration == null)
     {
         if (type.IsDelegate())
         {
             var resultType = type.GetMethod("Invoke").ReturnType;
             var nestedRegistration = ResolveNeedBy(resultType, key);
             if (nestedRegistration == null) return null;
             registration = new DelegateImpl(key, type, nestedRegistration);
         }
         else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Lazy<>))
         {
             var resultType = type.GetGenericArguments()[0];
             var nestedRegistration = ResolveNeedBy(resultType, key);
             if (nestedRegistration == null) return null;
             registration = new LazyImpl(type, nestedRegistration);
         }
         else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
         {
             var resultType = type.GetGenericArguments()[0];
             var child = MakeEnumEnabledChild();
             var nestedRegistration = child.ResolveNeedBy(resultType, key);
             if (nestedRegistration == null)
             {
                 if (key != null) return null;
                 registration = new EmptyEnumerableImpl(type, resultType);
             }
             else
             {
                 registration = new EnumerableImpl(key, type, resultType, child, nestedRegistration);
             }
         }
         else if (type.IsArray && type.GetArrayRank() == 1)
         {
             var resultType = type.GetElementType();
             var child = MakeEnumEnabledChild();
             var nestedRegistration = child.ResolveNeedBy(resultType, key);
             if (nestedRegistration == null) return null;
             registration = new EnumerableImpl(key, type, resultType, child, nestedRegistration);
         }
         else if (type.IsGenericType && TupleTypes.Contains(type.GetGenericTypeDefinition()))
         {
             registration = new AlwaysNewImpl(type, type.GetConstructors()[0]);
         }
     }
     if (registration != null)
     {
         var result = registration as ICRegILGen;
         if (result != null) return result;
         throw new ArgumentException("Builder for " + type.ToSimpleName() + " is not ILGen capable");
     }
     return null;
 }
Esempio n. 2
0
 public ICRegILGen?ResolveNeedBy(Type type, object?key)
 {
     if (_container.Registrations.TryGetValue(new KeyAndType(key, type), out var registration))
     {
         if (registration is ICRegMulti multi)
         {
             registration = ChooseFromMulti(multi, false) ?? multi.ChosenOne;
         }
     }
     if (registration == null)
     {
         if (type.IsDelegate())
         {
             var resultType         = type.GetMethod("Invoke") !.ReturnType;
             var nestedRegistration = ResolveNeedBy(resultType, key);
             if (nestedRegistration == null)
             {
                 return(null);
             }
             registration = new DelegateImpl(key, type, nestedRegistration);
         }
         else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Lazy <>))
         {
             var resultType         = type.GetGenericArguments()[0];
             var nestedRegistration = ResolveNeedBy(resultType, key);
             if (nestedRegistration == null)
             {
                 return(null);
             }
             registration = new LazyImpl(type, nestedRegistration);
         }
         else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable <>))
         {
             var resultType         = type.GetGenericArguments()[0];
             var child              = MakeEnumEnabledChild();
             var nestedRegistration = child.ResolveNeedBy(resultType, key);
             if (nestedRegistration == null)
             {
                 if (key != null)
                 {
                     return(null);
                 }
                 registration = new EmptyEnumerableImpl(type, resultType);
             }
             else
             {
                 registration = new EnumerableImpl(key, type, resultType, child, nestedRegistration);
             }
         }
         else if (type.IsArray && type.GetArrayRank() == 1)
         {
             var resultType         = type.GetElementType();
             var child              = MakeEnumEnabledChild();
             var nestedRegistration = child.ResolveNeedBy(resultType !, key);
             if (nestedRegistration == null)
             {
                 return(null);
             }
             registration = new EnumerableImpl(key, type, resultType, child, nestedRegistration);
         }
         else if (type.IsGenericType && TupleTypes.Contains(type.GetGenericTypeDefinition()))
         {
             registration = new AlwaysNewImpl(type, type.GetConstructors()[0], false);
         }
     }
     if (registration != null)
     {
         if (registration is ICRegILGen result)
         {
             return(result);
         }
         throw new ArgumentException("Builder for " + type.ToSimpleName() + " is not ILGen capable");
     }
     return(null);
 }