Beispiel #1
0
        public static object CreateInstance(Type type, params object[] args)
        {
            CType ct = type;

            if (args == null || args.Length == 0)
            {
                return(Activator.CreateInstance(ct));
            }

            var ci = ct.GetConstructor(CTypeExtensions.GetTypeArray(args));

            if (ci == null)
            {
                if (Array.IndexOf(args, null) != -1)
                {
                    var cis = ct.GetConstructors();
                    if (cis.Length == 1)
                    {
                        var parms = cis[0].GetParameters();
                        if (parms.Length == args.Length)
                        {
                            for (int ix = 0; ix < parms.Length; ++ix)
                            {
                                if (args[ix] != null)
                                {
                                    continue;
                                }

                                if (parms[ix].ParameterType.IsPrimitive)
                                {
                                    break;
                                }
                            }

                            ci = cis[0];
                        }
                    }
                }

                if (ci == null)
                {
                    throw new MissingMethodException();
                }
            }

            return(ci.Invoke(args));
        }
Beispiel #2
0
        public static object CreateInstance(CType type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture)
        {
            var ci = type.GetConstructor(bindingAttr, binder, CTypeExtensions.GetTypeArray(args), null);

            if (ci == null)
            {
                if (Array.IndexOf(args, null) != -1)
                {
                    var cis = type.GetConstructors(bindingAttr);
                    if (cis.Length == 1)
                    {
                        var parms = cis[0].GetParameters();
                        if (parms.Length == args.Length)
                        {
                            for (int ix = 0; ix < parms.Length; ++ix)
                            {
                                if (args[ix] != null)
                                {
                                    continue;
                                }

                                if (parms[ix].ParameterType.IsPrimitive)
                                {
                                    break;
                                }
                            }

                            ci = cis[0];
                        }
                    }
                }

                if (ci == null)
                {
                    throw new MissingMethodException();
                }
            }

            return(ci.Invoke(bindingAttr, binder, args, culture));
        }
 public static MethodInfo GetRuntimeMethod(this CType type, String name, Type[] parameters)
 {
     return(type.GetCType().GetMethod(name, CTypeExtensions.MakeTypeArray(parameters)));
 }