SmartDIContainer.cs
/// <summary>
/// Tries to create an object and reports the result.
/// </summary>
/// <param name="qualifyingMasterType">Type of the qualifying master.</param>
/// <param name="instantiatedObject">The instantiated object.</param>
/// <returns><c>true</c> if object was created successfully, else <c>false</c>.</returns>
Tries to create an object and reports the result.
Tries to create an object and reports the result .
private bool CouldNotCreateObject
      (
         Type       qualifyingMasterType,
         ref object instantiatedObject
      )
      {
         if (instantiatedObject == null)
         {
            var availableConstructors =
               qualifyingMasterType.GetConstructors().OrderBy(c => c.GetParameters().Count()).ToArray();
            if (availableConstructors.IsEmpty())
            {
               instantiatedObject = Activator.CreateInstance(qualifyingMasterType);
               return false;
            }
            foreach (var constructor in availableConstructors)
            {
               var constructorParameters = constructor.GetParameters().ToArray();
               if (constructorParameters.IsEmpty())
               {
                  instantiatedObject = Activator.CreateInstance(qualifyingMasterType);
                  break;
               }
               var paramsToInject                  = new List<object>();
               var requiredParameterHasBeenSkipped = false;
               foreach (var parameterInfo in constructorParameters)
               {
                  object variableToInjectAsParameter = null;
                  var wasIgnoreResolveError = IgnoreResolveError;
                  IgnoreResolveError          = true;
                  variableToInjectAsParameter = Resolve(parameterInfo.ParameterType);
                  IgnoreResolveError          = wasIgnoreResolveError;
                  requiredParameterHasBeenSkipped =
                     (variableToInjectAsParameter == null) && !parameterInfo.IsOptional;
                  if (requiredParameterHasBeenSkipped)
                  {
                     break;
                  }
                  paramsToInject.Add(variableToInjectAsParameter ?? parameterInfo.DefaultValue);
               }
               if (requiredParameterHasBeenSkipped)
               {
                  continue;
               }
                Try to instantiate
               instantiatedObject = constructor.Invoke(paramsToInject.ToArray());
               if (instantiatedObject != null)
               {
                  break;
               }
            }
         }
         if (instantiatedObject == null)
         {
            return true;
         }
         return false;
      }
private bool CouldNotCreateObject ( Type qualifyingMasterType , ref object instantiatedObject ) { if ( instantiatedObject == null ) { var availableConstructors = qualifyingMasterType . GetConstructors ( ) . OrderBy ( c => c . GetParameters ( ) . Count ( ) ) . ToArray ( ) ; if ( availableConstructors . IsEmpty ( ) ) { instantiatedObject = Activator . CreateInstance ( qualifyingMasterType ) ; return false ; } foreach ( var constructor in availableConstructors ) { var constructorParameters = constructor . GetParameters ( ) . ToArray ( ) ; if ( constructorParameters . IsEmpty ( ) ) { instantiatedObject = Activator . CreateInstance ( qualifyingMasterType ) ; break ; } var paramsToInject = new List < object > ( ) ; var requiredParameterHasBeenSkipped = false ; foreach ( var parameterInfo in constructorParameters ) { object variableToInjectAsParameter = null ; var wasIgnoreResolveError = IgnoreResolveError ; IgnoreResolveError = true ; variableToInjectAsParameter = Resolve ( parameterInfo . ParameterType ) ; IgnoreResolveError = wasIgnoreResolveError ; requiredParameterHasBeenSkipped = ( variableToInjectAsParameter == null ) && ! parameterInfo . IsOptional ; if ( requiredParameterHasBeenSkipped ) { break ; } paramsToInject . Add ( variableToInjectAsParameter ?? parameterInfo . DefaultValue ) ; } if ( requiredParameterHasBeenSkipped ) { continue ; } instantiatedObject = constructor . Invoke ( paramsToInject . ToArray ( ) ) ; if ( instantiatedObject != null ) { break ; } } } if ( instantiatedObject == null ) { return true ; } return false ; }
Tries to create an object and reports the result.
Tries to create an object and reports the result .
// No choice but to use activator create instance Note that we actually *create* the qualifyingMasterType and // then *save as* the finalTypeRequestedT // Get the constructors and order by the fewest parameters -- HACK -- not very smart // ReSharper disable once UseCollectionCountProperty -- will not compile as the property "Count" // Ironically, this is a success case. // The object is created with Activator.CreateInstance. // Did *not* fail // try each constructor one at a time until we succeed // ReSharper disable once PossibleNullReferenceException - //     physically impossible based on "the previous statement" two lines before // If optional, do not throw Resolve errors // FAIL CASE because we can never skip a required parameter. // ELSE // Only add if found; could be an optional parameter. // FAIL CASE because we can never skip a required parameter. // ELSE Try to instantiate // If success, no need to try other constructors. // Inverted logic; could *not* get the object // Inverted logic; success case.
None
qualifyingMasterType
Type
Type of the qualifying master.
Type of the qualifying master .
object
ref
instantiatedObject
The instantiated object.
The instantiated object .