private IObjectConfiguration GetObjectConfiguration(string name, IContainer container)
        {
            IObjectConfiguration objectConfig = container.Configuration.GetObjectConfiguration(name);
            if (objectConfig == null)
            {
                objectConfig = new ObjectConfiguration();
                objectConfig.Name = name;
                container.Configuration.AddObjectConfiguration(objectConfig);
            }

            return objectConfig;
        }
        private ConstructorInfo MatchConstructors(ObjectConfiguration objectConfig)
        {
            try
            {
                ConstructorInfo[] constructors = objectConfig.Type.GetConstructors();
                ConstructorInfo bestCtorMatch = null;
                int bestScore = int.MaxValue;

                foreach (ConstructorInfo constructor in constructors)
                {
                    int score = 0;
                    ParameterInfo[] ctorParameters = constructor.GetParameters();

                    //if param count doesnt match , move to next ctor
                    if (ctorParameters.Length != objectConfig.CtorParameterConfigurations.Count)
                        continue;

                    if (ctorParameters.Length == 0)
                        return constructor;

                    #region Match parameters

                    for (int i = 0; i < ctorParameters.Length; i++)
                    {
                        ParameterConfiguration parameterConfig = (ParameterConfiguration) objectConfig.CtorParameterConfigurations[i];
                        Type type1 = ctorParameters[i].ParameterType;
                        Type type2 = parameterConfig.Type;
                        if (type2 == null)
                        {
                            ValueConfiguration parameterValueConfig = (ValueConfiguration) parameterConfig.Value;

                            if (parameterValueConfig.TypeConverter != null)
                            {
                                if (parameterValueConfig.TypeConverter.CanConvertTo(type1))
                                {
                                    score++;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                //untyped parameter
                                try
                                {
                                    if (type1.IsEnum && parameterValueConfig.Value is string)
                                    {
                                        object res = Enum.Parse(type1, parameterValueConfig.Value.ToString());
                                    }
                                    else
                                    {
                                        object res = Convert.ChangeType(parameterValueConfig.Value, type1);
                                    }
                                }
                                catch
                                {
                                    break;
                                }
                                score ++;
                            }
                        }
                        else
                        {
                            //typed parameter

                            if (type1.IsAssignableFrom(type2))
                            {
                                if (type1 == type2)
                                {
                                    //same type
                                    score ++;
                                }
                                else if (type2.IsSubclassOf(type1))
                                {
                                    //subclass
                                    Type tmpType = type2;
                                    while (tmpType != type1)
                                    {
                                        score++;
                                        tmpType = tmpType.BaseType;
                                    }
                                }
                                else
                                {
                                    //interface
                                    score ++;
                                }
                            }
                            else
                            {
                                //ignore this
                                break;
                            }
                        }
                    }

                    #endregion

                    if (score < bestScore && score != 0)
                    {
                        bestCtorMatch = constructor;
                        bestScore = score;
                    }
                }
                return bestCtorMatch;
            }
            catch (Exception x)
            {
                throw new Exception(string.Format("could not match constructor for object '{0}'", objectConfig.Name), x);
            }
        }
 private void FillCtorParameterTypes(ObjectConfiguration objectConfig)
 {
     foreach (ParameterConfiguration ctorParameter in objectConfig.CtorParameterConfigurations)
     {
         if (ctorParameter.Value is ObjectConfiguration)
         {
             ObjectConfiguration referenceObject = ctorParameter.Value as ObjectConfiguration;
             ctorParameter.Type = referenceObject.Type;
         }
     }
 }