Пример #1
0
 public static object CreateFromName(string name, params object[] args)
 {
   if (name == null)
     throw new ArgumentNullException("name");
   Type type = (Type) null;
   CryptoConfig.InitializeConfigInfo();
   lock (CryptoConfig.InternalSyncObject)
     type = CryptoConfig.appNameHT.GetValueOrDefault(name);
   if (type == (Type) null)
   {
     string valueOrDefault = CryptoConfig.machineNameHT.GetValueOrDefault(name);
     if (valueOrDefault != null)
     {
       type = Type.GetType(valueOrDefault, false, false);
       if (type != (Type) null && !type.IsVisible)
         type = (Type) null;
     }
   }
   if (type == (Type) null)
   {
     object valueOrDefault = CryptoConfig.DefaultNameHT.GetValueOrDefault(name);
     if (valueOrDefault != null)
     {
       if (valueOrDefault is Type)
         type = (Type) valueOrDefault;
       else if (valueOrDefault is string)
       {
         type = Type.GetType((string) valueOrDefault, false, false);
         if (type != (Type) null && !type.IsVisible)
           type = (Type) null;
       }
     }
   }
   if (type == (Type) null)
   {
     type = Type.GetType(name, false, false);
     if (type != (Type) null && !type.IsVisible)
       type = (Type) null;
   }
   if (type == (Type) null)
     return (object) null;
   RuntimeType runtimeType = type as RuntimeType;
   if (runtimeType == (RuntimeType) null)
     return (object) null;
   if (args == null)
     args = new object[0];
   MethodBase[] methodBaseArray = (MethodBase[]) runtimeType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance);
   if (methodBaseArray == null)
     return (object) null;
   List<MethodBase> methodBaseList = new List<MethodBase>();
   for (int index = 0; index < methodBaseArray.Length; ++index)
   {
     MethodBase methodBase = methodBaseArray[index];
     if (methodBase.GetParameters().Length == args.Length)
       methodBaseList.Add(methodBase);
   }
   if (methodBaseList.Count == 0)
     return (object) null;
   object state;
   RuntimeConstructorInfo runtimeConstructorInfo = Type.DefaultBinder.BindToMethod(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, methodBaseList.ToArray(), ref args, (ParameterModifier[]) null, (CultureInfo) null, (string[]) null, out state) as RuntimeConstructorInfo;
   if ((ConstructorInfo) runtimeConstructorInfo == (ConstructorInfo) null || typeof (Delegate).IsAssignableFrom(runtimeConstructorInfo.DeclaringType))
     return (object) null;
   object obj = runtimeConstructorInfo.Invoke(BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, Type.DefaultBinder, args, (CultureInfo) null);
   if (state == null)
     return obj;
   Type.DefaultBinder.ReorderArgumentArray(ref args, state);
   return obj;
 }
        public static object CreateFromName(string name, params object[] args)
        {
            object obj4;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            Type valueOrDefault = null;

            InitializeConfigInfo();
            lock (InternalSyncObject)
            {
                valueOrDefault = appNameHT.GetValueOrDefault(name);
            }
            if (valueOrDefault == null)
            {
                string typeName = machineNameHT.GetValueOrDefault(name);
                if (typeName != null)
                {
                    valueOrDefault = Type.GetType(typeName, false, false);
                    if ((valueOrDefault != null) && !valueOrDefault.IsVisible)
                    {
                        valueOrDefault = null;
                    }
                }
            }
            if (valueOrDefault == null)
            {
                object obj3 = DefaultNameHT.GetValueOrDefault(name);
                if (obj3 != null)
                {
                    if (obj3 is Type)
                    {
                        valueOrDefault = (Type)obj3;
                    }
                    else if (obj3 is string)
                    {
                        valueOrDefault = Type.GetType((string)obj3, false, false);
                        if ((valueOrDefault != null) && !valueOrDefault.IsVisible)
                        {
                            valueOrDefault = null;
                        }
                    }
                }
            }
            if (valueOrDefault == null)
            {
                valueOrDefault = Type.GetType(name, false, false);
                if ((valueOrDefault != null) && !valueOrDefault.IsVisible)
                {
                    valueOrDefault = null;
                }
            }
            if (valueOrDefault == null)
            {
                return(null);
            }
            RuntimeType type2 = valueOrDefault as RuntimeType;

            if (type2 == null)
            {
                return(null);
            }
            if (args == null)
            {
                args = new object[0];
            }
            MethodBase[] constructors = type2.GetConstructors(BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance);
            if (constructors == null)
            {
                return(null);
            }
            List <MethodBase> list = new List <MethodBase>();

            for (int i = 0; i < constructors.Length; i++)
            {
                MethodBase item = constructors[i];
                if (item.GetParameters().Length == args.Length)
                {
                    list.Add(item);
                }
            }
            if (list.Count == 0)
            {
                return(null);
            }
            constructors = list.ToArray();
            RuntimeConstructorInfo info = Type.DefaultBinder.BindToMethod(BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance, constructors, ref args, null, null, null, out obj4) as RuntimeConstructorInfo;

            if ((info == null) || typeof(Delegate).IsAssignableFrom(info.DeclaringType))
            {
                return(null);
            }
            object obj2 = info.Invoke(BindingFlags.CreateInstance | BindingFlags.Public | BindingFlags.Instance, Type.DefaultBinder, args, null);

            if (obj4 != null)
            {
                Type.DefaultBinder.ReorderArgumentArray(ref args, obj4);
            }
            return(obj2);
        }
Пример #3
0
        public static Object CreateFromName(String name, Object[] args)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            Type   retvalType = null;
            Object retval;

            // First we'll do the machine-wide stuff, initializing if necessary
            if (!isInitialized)
            {
                InitializeConfigInfo();
            }

            // Search the machine table

            if (machineNameHT != null)
            {
                String retvalTypeString = (String)machineNameHT[name];
                if (retvalTypeString != null)
                {
                    retvalType = RuntimeType.GetTypeInternal(retvalTypeString, false, false, true);
                }
            }

            // If we didn't find it in the machine-wide table,  look in the default table
            if (retvalType == null)
            {
                // We allow the default table to Types and Strings
                // Types get used for other stuff in mscorlib.dll
                // strings get used for delay-loaded stuff like System.Security.dll
                Object retvalObj = defaultNameHT[name];
                if (retvalObj != null)
                {
                    if (retvalObj is Type)
                    {
                        retvalType = (Type)retvalObj;
                    }
                    else if (retvalObj is String)
                    {
                        retvalType = RuntimeType.GetTypeInternal((String)retvalObj, false, false, true);
                    }
                }
            }

            // Maybe they gave us a classname.
            if (retvalType == null)
            {
                retvalType = RuntimeType.GetTypeInternal(name, false, false, true);
            }

            // Still null?  Then we didn't find it
            if (retvalType == null)
            {
                return(null);
            }

            // Perform a CreateInstance by hand so we can check that the
            // constructor doesn't have a linktime demand attached (which would
            // be incorrrectly applied against mscorlib otherwise).
            RuntimeType rtType = retvalType as RuntimeType;

            if (rtType == null)
            {
                return(null);
            }
            if (args == null)
            {
                args = new Object[] {}
            }
            ;

            // Locate all constructors.
            bool isDelegate;

            MethodBase[] cons = rtType.GetMemberCons(Activator.ConstructorDefault,
                                                     CallingConventions.Any,
                                                     null,
                                                     args.Length,
                                                     false,
                                                     out isDelegate);
            if (cons == null)
            {
                return(null);
            }

            // Bind to matching ctor.
            Object state;
            RuntimeConstructorInfo rci = Type.DefaultBinder.BindToMethod(Activator.ConstructorDefault,
                                                                         cons,
                                                                         ref args,
                                                                         null,
                                                                         null,
                                                                         null,
                                                                         out state) as RuntimeConstructorInfo;

            // Check for ctor we don't like (non-existant, delegate or decorated
            // with declarative linktime demand).
            if (rci == null || isDelegate)
            {
                return(null);
            }

            // Ctor invoke (actually causes the allocation as well).
            retval = rci.Invoke(Activator.ConstructorDefault, Type.DefaultBinder, args, null);

            // Reset any parameter re-ordering performed by the binder.
            if (state != null)
            {
                Type.DefaultBinder.ReorderArgumentArray(ref args, state);
            }

            return(retval);
        }