Esempio n. 1
0
        internal object CreateInstance(IComponentContext context)
        {
            ConstructorInfo constructorInfo = null;
            var             impType         = context.Component.Implementation;

            if (impType.IsAbstract || impType.IsInterface)
            {
                throw new ActivatorException(
                          string.Format(Mini_Resources.TypeAbstract,
                                        impType.FullName,
                                        context.Component.Contracts[0].FullName));
            }

            if (impType.IsOpenGenericType())
            {
                impType = impType.MakeCloseGenericType(context.GenericParameters);
            }

            if (context.NamedArgs != null && context.NamedArgs.Count > 0)
            {
                constructorInfo = impType.FindEligibleConstructor(context.NamedArgs);
                if (impType.IsValueType)
                {
                    return(Activator.CreateInstance(impType));
                }
                context.Instance = constructorInfo.FastInvoke(context.NamedArgs.Values.ToArray());
            }
            else if (context.Args != null && context.Args.Length > 0)
            {
                constructorInfo = impType.FindEligibleConstructor(context.Args);
                if (impType.IsValueType)
                {
                    return(Activator.CreateInstance(impType));
                }
                context.Instance = constructorInfo.FastInvoke(context.Args);
            }
            else
            {
                const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public;
                var ctors = context.Component.ExtendedProperties[impType.FullName + ":ctorInjections"] as ConstructorInjection[];
                if (ctors != null && ctors.Length > 0)
                {
                    var validConstructorBinding = ctors.FirstOrDefault(p => p.IsMatch);
                    if (validConstructorBinding == null)
                    {
                        throw new ActivatorException(
                                  string.Format(Mini_Resources.NoConstructorsAvailable, impType, bindingFlags));
                    }

                    validConstructorBinding.Inject(context);
                }
                else
                {
                    if (impType.IsValueType)
                    {
                        return(Activator.CreateInstance(impType));
                    }


                    throw new ActivatorException(
                              string.Format(Mini_Resources.NoConstructorsAvailable, impType, bindingFlags));
                }
            }

            return(context.Instance);
        }
Esempio n. 2
0
 /// <summary>通过类的构造器信息创建对象实例</summary>
 /// <typeparam name="T">返回对象类型</typeparam>
 /// <param name="constructorInfo">构造器信息</param>
 /// <param name="parameters">参数</param>
 /// <returns>新对象实例</returns>
 public static T FastInvoke <T>(this ConstructorInfo constructorInfo, params object[] parameters)
 {
     return((T)constructorInfo.FastInvoke(parameters));
 }
Esempio n. 3
0
 static object ConstructorInvoke(ConstructorInfo ctor, object[] parameterValues)
 {
     return(ctor.FastInvoke(parameterValues));
 }