Пример #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;
 }
Пример #2
0
        // GetRealObject intercepts the deserialization process in order to allow deserializing part of the
        // object's inheritance heirarchy using standard ISerializable constructors, and the remaining
        // portion using the saved serialization states.
        object IObjectReference.GetRealObject(StreamingContext context)
        {
            // If we've already deserialized the real object, use that rather than deserializing it again
            if (m_realObject != null)
            {
                return(m_realObject);
            }

            // If we don't have a real type to deserialize, then this is really a SafeSerializationManager
            // and we don't need to rebuild the object that we're standing in for.
            if (m_realType == null)
            {
                return(this);
            }

            // Look for the last type in GetRealType's inheritance hierarchy which implements a critical
            // deserialization constructor.  This will be the object that we use as the deserialization
            // construction type to initialize via standard ISerializable semantics

            // First build up the chain starting at the type below Object and working to the real type we
            // serialized.
            Stack       inheritanceChain = new Stack();
            RuntimeType currentType      = m_realType;

            do
            {
                inheritanceChain.Push(currentType);
                currentType = currentType.BaseType as RuntimeType;
            }while (currentType != typeof(object));

            // Now look for the first type that does not implement the ISerializable .ctor.  When we find
            // that, previousType will point at the last type that did implement the .ctor.  We require that
            // the .ctor we invoke also be non-transparent
            RuntimeConstructorInfo serializationCtor = null;
            RuntimeType            previousType      = null;

            do
            {
                previousType      = currentType;
                currentType       = inheritanceChain.Pop() as RuntimeType;
                serializationCtor = currentType.GetSerializationCtor();
            }while (serializationCtor != null && serializationCtor.IsSecurityCritical);

            // previousType is the last type that did implement the deserialization .ctor before the first
            // type that did not, so we'll grab it's .ctor to use for deserialization.
            BCLDebug.Assert(previousType != null, "We should have at least one inheritance from the base type");
            serializationCtor = ObjectManager.GetConstructor(previousType);

            // Allocate an instance of the final type and run the selected .ctor on that instance to get the
            // standard ISerializable initialization done.
            object deserialized = FormatterServices.GetUninitializedObject(m_realType);

            serializationCtor.SerializationInvoke(deserialized, m_savedSerializationInfo, context);
            m_savedSerializationInfo = null;
            m_realType = null;

            // Save away the real object that was deserialized so that we can fill it in later, and return
            // it back as the object that should result from the final deserialization.
            m_realObject = deserialized;
            return(deserialized);
        }
Пример #3
0
 private RuntimeCLSIDTypeInfo(Guid clsid, string server)
 {
     _key          = new UnificationKey(clsid, server);
     _constructors = new RuntimeConstructorInfo[] { RuntimeCLSIDNullaryConstructorInfo.GetRuntimeCLSIDNullaryConstructorInfo(this) };
 }
Пример #4
0
 private int GetTokenFor(RuntimeConstructorInfo rtMeth)
 {
     return(m_scope.GetTokenFor(rtMeth.MethodHandle));
 }
Пример #5
0
 private int GetTokenFor(RuntimeConstructorInfo rtMeth, RuntimeType rtType)
 {
     return(m_scope.GetTokenFor(rtMeth.MethodHandle, rtType.TypeHandle));
 }
Пример #6
0
        /// <summary>确定与指定的 SOAPAction 值关联的方法的类型和方法名。</summary>
        /// <returns>如果类型和方法名成功恢复,则为 true;否则为 false。</returns>
        /// <param name="soapAction">为其请求类型和方法名的方法的 SOAPAction。</param>
        /// <param name="typeName">当此方法返回时,该参数包含保存了相关方法的类型名称的 <see cref="T:System.String" />。该参数未经初始化即被传递。</param>
        /// <param name="methodName">当此方法返回时,该参数包含保存了相关方法的方法名的 <see cref="T:System.String" />。该参数未经初始化即被传递。</param>
        /// <exception cref="T:System.Runtime.Remoting.RemotingException">SOAPAction 值不以引号开头和结尾。</exception>
        /// <exception cref="T:System.Security.SecurityException">直接调用方没有基础结构权限。</exception>
        /// <PermissionSet>
        ///   <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="Infrastructure" />
        /// </PermissionSet>
        public static bool GetTypeAndMethodNameFromSoapAction(string soapAction, out string typeName, out string methodName)
        {
            if ((int)soapAction[0] == 34)
            {
                string str   = soapAction;
                int    index = str.Length - 1;
                if ((int)str[index] == 34)
                {
                    soapAction = soapAction.Substring(1, soapAction.Length - 2);
                }
            }
            ArrayList arrayList = (ArrayList)SoapServices._soapActionToMethodBase[(object)soapAction];

            if (arrayList != null)
            {
                if (arrayList.Count > 1)
                {
                    typeName   = (string)null;
                    methodName = (string)null;
                    return(false);
                }
                MethodBase methodBase = (MethodBase)arrayList[0];
                if (methodBase != (MethodBase)null)
                {
                    RuntimeMethodInfo      runtimeMethodInfo      = methodBase as RuntimeMethodInfo;
                    RuntimeConstructorInfo runtimeConstructorInfo = methodBase as RuntimeConstructorInfo;
                    RuntimeModule          runtimeModule;
                    if ((MethodInfo)runtimeMethodInfo != (MethodInfo)null)
                    {
                        runtimeModule = runtimeMethodInfo.GetRuntimeModule();
                    }
                    else
                    {
                        if (!((ConstructorInfo)runtimeConstructorInfo != (ConstructorInfo)null))
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeReflectionObject"));
                        }
                        runtimeModule = runtimeConstructorInfo.GetRuntimeModule();
                    }
                    typeName   = methodBase.DeclaringType.FullName + ", " + runtimeModule.GetRuntimeAssembly().GetSimpleName();
                    methodName = methodBase.Name;
                    return(true);
                }
            }
            string[] strArray = soapAction.Split('#');
            if (strArray.Length == 2)
            {
                bool assemblyIncluded;
                typeName = XmlNamespaceEncoder.GetTypeNameForSoapActionNamespace(strArray[0], out assemblyIncluded);
                if (typeName == null)
                {
                    methodName = (string)null;
                    return(false);
                }
                methodName = strArray[1];
                return(true);
            }
            typeName   = (string)null;
            methodName = (string)null;
            return(false);
        }
Пример #7
0
        public static bool IsSoapActionValidForMethodBase(string soapAction, MethodBase mb)
        {
            if (mb == (MethodBase)null)
            {
                throw new ArgumentNullException("mb");
            }
            if ((int)soapAction[0] == 34)
            {
                string str   = soapAction;
                int    index = str.Length - 1;
                if ((int)str[index] == 34)
                {
                    soapAction = soapAction.Substring(1, soapAction.Length - 2);
                }
            }
            if (string.CompareOrdinal(((SoapMethodAttribute)InternalRemotingServices.GetCachedSoapAttribute((object)mb)).SoapAction, soapAction) == 0)
            {
                return(true);
            }
            string strA = (string)SoapServices._methodBaseToSoapAction[(object)mb];

            if (strA != null && string.CompareOrdinal(strA, soapAction) == 0)
            {
                return(true);
            }
            string[] strArray = soapAction.Split('#');
            if (strArray.Length != 2)
            {
                return(false);
            }
            bool   assemblyIncluded;
            string soapActionNamespace = XmlNamespaceEncoder.GetTypeNameForSoapActionNamespace(strArray[0], out assemblyIncluded);

            if (soapActionNamespace == null)
            {
                return(false);
            }
            string                 str1 = strArray[1];
            RuntimeMethodInfo      runtimeMethodInfo      = mb as RuntimeMethodInfo;
            RuntimeConstructorInfo runtimeConstructorInfo = mb as RuntimeConstructorInfo;
            RuntimeModule          runtimeModule;

            if ((MethodInfo)runtimeMethodInfo != (MethodInfo)null)
            {
                runtimeModule = runtimeMethodInfo.GetRuntimeModule();
            }
            else
            {
                if (!((ConstructorInfo)runtimeConstructorInfo != (ConstructorInfo)null))
                {
                    throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeReflectionObject"));
                }
                runtimeModule = runtimeConstructorInfo.GetRuntimeModule();
            }
            string str2 = mb.DeclaringType.FullName;

            if (assemblyIncluded)
            {
                str2 = str2 + ", " + runtimeModule.GetRuntimeAssembly().GetSimpleName();
            }
            if (str2.Equals(soapActionNamespace))
            {
                return(mb.Name.Equals(str1));
            }
            return(false);
        }
Пример #8
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);
        }
 internal static bool TryGetConstructor(RuntimeType t, RuntimeType[] ctorParams, out RuntimeConstructorInfo ctorInfo)
 {
     ctorInfo = t.RemotingCache[CacheObjType.ConstructorInfo] as RuntimeConstructorInfo;
     if (ctorInfo != null)
     {
         return(true);
     }
     ctorInfo = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, ctorParams, null) as RuntimeConstructorInfo;
     if (ctorInfo != null)
     {
         t.RemotingCache[CacheObjType.ConstructorInfo] = ctorInfo;
         return(true);
     }
     return(false);
 }
 internal static bool TryGetConstructor(RuntimeType t, out RuntimeConstructorInfo ctorInfo)
 {
     return(TryGetConstructor(t, SIConstructorTypes, out ctorInfo));
 }