Ejemplo n.º 1
0
 private static object AttemptConstruction(object[] parameters, IEnumerable <KeyValuePair <ConstructorInfo, ParameterInfo[]> > ctors, bool matchExactType)
 {
     foreach (var info in ctors)
     {
         var parametersCopy = new object[info.Value.Length];
         for (var i = 0; i < info.Value.Length; i++)
         {
             var param = info.Value[i];
             if (parameters != null && parameters.Length > i)
             {
                 var item = parameters[i];
                 if (item != null && (matchExactType ? param.ParameterType != item.GetType()
                     : !param.ParameterType.GetTypeInfo().IsAssignableFrom(item.GetType().GetTypeInfo())))
                 {
                     parametersCopy = null;
                     break;
                 }
                 parametersCopy[i] = item;
             }
             else if ((param.Attributes & ParameterAttributes.HasDefault) == ParameterAttributes.HasDefault)
             {
                 parametersCopy[i] = param.DefaultValue;
             }
             else
             {
                 parametersCopy[i] = MXContainer.Resolve(param.ParameterType, null, null);
                 if (parametersCopy[i] != null)
                 {
                     continue;
                 }
                 parametersCopy = null;
                 break;
             }
         }
         if (parametersCopy != null)
         {
             return(info.Key.Invoke(parametersCopy));
         }
     }
     return(null);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the managed instance.
        /// </summary>
        /// <param name="parameters">An array of constructor parameters for initialization.</param>
        /// <returns>The object instance.</returns>
        public object Load(params object[] parameters)
        {
            if (_instanceType == null)
            {
                return(null);
            }

            if (Instance != null)
            {
                return(Instance);
            }

            object retval;

            if (_initialize == null)
            {
                try
                {
                    var ctors = _instanceType.GetTypeInfo().DeclaredConstructors;
                    if (parameters == null || parameters.Length == 0)
                    {
                        var info = ctors.Select(c => new { ctor = c, param = c.GetParameters() })
                                   .OrderBy(i => i.param.Length)
                                   .Select(p => new { p.ctor, param = p.param.Select(res => MXContainer.Resolve(res.ParameterType, null, null)) })
                                   .FirstOrDefault(v => v.param.All(o => o != null));

                        retval = info != null?info.ctor.Invoke(info.param.ToArray()) : Activator.CreateInstance(_instanceType);
                    }
                    else
                    {
                        var ctor = ctors.FirstOrDefault(c =>
                        {
                            var p = c.GetParameters();
                            return(p.Length == parameters.Length && !p.Where((t, i) => parameters[i] != null && !t.ParameterType.GetTypeInfo().IsAssignableFrom(parameters[i].GetType().GetTypeInfo())).Any());
                        });
                        retval = ctor == null?Activator.CreateInstance(_instanceType) : ctor.Invoke(parameters);
                    }
                }
                catch (MissingMemberException)
                {
                    retval = Activator.CreateInstance(_instanceType);
                }
            }
            else
            {
                retval = _initialize(parameters);
            }

            if (_singletonInstance)
            {
                _instance = retval;
            }

            return(retval);
        }