GetTypeFromCode() public static method

public static GetTypeFromCode ( int code ) : Type
code int
return System.Type
示例#1
0
        public Type ReadType(BinaryReader reader, TypeTag code)
        {
            switch (code)
            {
            case TypeTag.PrimitiveType:
                return(BinaryCommon.GetTypeFromCode(reader.ReadByte()));

            case TypeTag.String:
                return(typeof(string));

            case TypeTag.ObjectType:
                return(typeof(object));

            case TypeTag.RuntimeType:
            {
                string name = reader.ReadString();
#if NET_2_0
                // map MS.NET's System.RuntimeType to System.MonoType
                if (_context.State == StreamingContextStates.Remoting)
                {
                    if (name == "System.RuntimeType")
                    {
                        return(typeof(MonoType));
                    }
                    else if (name == "System.RuntimeType[]")
                    {
                        return(typeof(MonoType[]));
                    }
                }
#endif
                Type t = Type.GetType(name);
                if (t != null)
                {
                    return(t);
                }

                throw new SerializationException(String.Format("Could not find type '{0}'.", name));
            }

            case TypeTag.GenericType:
            {
                string name  = reader.ReadString();
                long   asmid = (long)reader.ReadUInt32();
                return(GetDeserializationType(asmid, name));
            }

            case TypeTag.ArrayOfObject:
                return(typeof(object[]));

            case TypeTag.ArrayOfString:
                return(typeof(string[]));

            case TypeTag.ArrayOfPrimitiveType:
                Type elementType = BinaryCommon.GetTypeFromCode(reader.ReadByte());
                return(Type.GetType(elementType.FullName + "[]"));

            default:
                throw new NotSupportedException("Unknow type tag");
            }
        }
示例#2
0
        public Type ReadType(BinaryReader reader, TypeTag code)
        {
            switch (code)
            {
            case TypeTag.PrimitiveType:
                return(BinaryCommon.GetTypeFromCode((int)reader.ReadByte()));

            case TypeTag.String:
                return(typeof(string));

            case TypeTag.ObjectType:
                return(typeof(object));

            case TypeTag.RuntimeType:
            {
                string text = reader.ReadString();
                if (this._context.State == StreamingContextStates.Remoting)
                {
                    if (text == "System.RuntimeType")
                    {
                        return(typeof(MonoType));
                    }
                    if (text == "System.RuntimeType[]")
                    {
                        return(typeof(MonoType[]));
                    }
                }
                Type type = Type.GetType(text);
                if (type != null)
                {
                    return(type);
                }
                throw new SerializationException(string.Format("Could not find type '{0}'.", text));
            }

            case TypeTag.GenericType:
            {
                string className  = reader.ReadString();
                long   assemblyId = (long)((ulong)reader.ReadUInt32());
                return(this.GetDeserializationType(assemblyId, className));
            }

            case TypeTag.ArrayOfObject:
                return(typeof(object[]));

            case TypeTag.ArrayOfString:
                return(typeof(string[]));

            case TypeTag.ArrayOfPrimitiveType:
            {
                Type typeFromCode = BinaryCommon.GetTypeFromCode((int)reader.ReadByte());
                return(Type.GetType(typeFromCode.FullName + "[]"));
            }

            default:
                throw new NotSupportedException("Unknow type tag");
            }
        }
        public static object ReadMethodResponse(BinaryElement elem, BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
        {
            if (elem != BinaryElement.MethodResponse)
            {
                throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
            }

            MethodFlags   flags          = (MethodFlags)reader.ReadByte();
            ReturnTypeTag typeTag        = (ReturnTypeTag)reader.ReadByte();
            bool          hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;

            // FIXME: find a meaning for those 2 bytes
            reader.ReadByte();
            reader.ReadByte();

            object returnValue = null;

            object[]           outArgs     = null;
            LogicalCallContext callContext = null;
            Exception          exception   = null;

            object[] extraProperties = null;
            Header[] headers         = null;

            if ((typeTag & ReturnTypeTag.PrimitiveType) > 0)
            {
                Type type = BinaryCommon.GetTypeFromCode(reader.ReadByte());
                returnValue = ObjectReader.ReadPrimitiveTypeValue(reader, type);
            }

            if ((flags & MethodFlags.PrimitiveArguments) > 0)
            {
                uint count = reader.ReadUInt32();
                outArgs = new object[count];
                for (int n = 0; n < count; n++)
                {
                    Type type = BinaryCommon.GetTypeFromCode(reader.ReadByte());
                    outArgs[n] = ObjectReader.ReadPrimitiveTypeValue(reader, type);
                }
            }

            if (hasContextInfo || (typeTag & ReturnTypeTag.ObjectType) > 0 ||
                (typeTag & ReturnTypeTag.Exception) > 0 ||
                (flags & MethodFlags.ArgumentsInSimpleArray) > 0 ||
                (flags & MethodFlags.ArgumentsInMultiArray) > 0)
            {
                // There objects that need to be deserialized using an ObjectReader

                ObjectReader objectReader = new ObjectReader(formatter);
                object       result;
                objectReader.ReadObjectGraph(reader, hasHeaders, out result, out headers);
                object[] msgInfo = (object[])result;

                if ((typeTag & ReturnTypeTag.Exception) > 0)
                {
                    exception = (Exception)msgInfo[0];
                    if (hasContextInfo)
                    {
                        callContext = (LogicalCallContext)msgInfo[1];
                    }
                }
                else if ((flags & MethodFlags.NoArguments) > 0 || (flags & MethodFlags.PrimitiveArguments) > 0)
                {
                    int n = 0;
                    if ((typeTag & ReturnTypeTag.ObjectType) > 0)
                    {
                        returnValue = msgInfo [n++];
                    }
                    if (hasContextInfo)
                    {
                        callContext = (LogicalCallContext)msgInfo[n++];
                    }
                    if (n < msgInfo.Length)
                    {
                        extraProperties = (object[])msgInfo[n];
                    }
                }
                else if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0)
                {
                    outArgs = msgInfo;
                }
                else
                {
                    int n = 0;
                    outArgs = (object[])msgInfo[n++];
                    if ((typeTag & ReturnTypeTag.ObjectType) > 0)
                    {
                        returnValue = msgInfo[n++];
                    }
                    if (hasContextInfo)
                    {
                        callContext = (LogicalCallContext)msgInfo[n++];
                    }
                    if (n < msgInfo.Length)
                    {
                        extraProperties = (object[])msgInfo[n];
                    }
                }
            }
            else
            {
                reader.ReadByte(); // Reads the stream ender
            }

            if (headerHandler != null)
            {
                headerHandler(headers);
            }

            if (exception != null)
            {
                return(new ReturnMessage(exception, methodCallMessage));
            }
            else
            {
                int           argCount = (outArgs != null) ? outArgs.Length : 0;
                ReturnMessage result   = new ReturnMessage(returnValue, outArgs, argCount, callContext, methodCallMessage);

                if (extraProperties != null)
                {
                    foreach (DictionaryEntry entry in extraProperties)
                    {
                        result.Properties [(string)entry.Key] = entry.Value;
                    }
                }

                return(result);
            }
        }
        public static object ReadMethodCall(BinaryElement elem, BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
        {
            if (elem != BinaryElement.MethodCall)
            {
                throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
            }

            MethodFlags flags = (MethodFlags)reader.ReadInt32();

            if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String)
            {
                throw new SerializationException("Invalid format");
            }
            string methodName = reader.ReadString();

            if (((BinaryTypeCode)reader.ReadByte()) != BinaryTypeCode.String)
            {
                throw new SerializationException("Invalid format");
            }
            string className = reader.ReadString();

            //bool hasContextInfo = (flags & MethodFlags.IncludesLogicalCallContext) > 0;

            object[] arguments       = null;
            object   methodSignature = null;
            object   callContext     = null;

            object[] extraProperties  = null;
            Header[] headers          = null;
            Type[]   genericArguments = null;

            if ((flags & MethodFlags.PrimitiveArguments) > 0)
            {
                uint count = reader.ReadUInt32();
                arguments = new object[count];
                for (int n = 0; n < count; n++)
                {
                    Type type = BinaryCommon.GetTypeFromCode(reader.ReadByte());
                    arguments[n] = ObjectReader.ReadPrimitiveTypeValue(reader, type);
                }
            }

            if ((flags & MethodFlags.NeedsInfoArrayMask) > 0)
            {
                ObjectReader objectReader = new ObjectReader(formatter);

                object result;
                objectReader.ReadObjectGraph(reader, hasHeaders, out result, out headers);
                object[] msgInfo = (object[])result;

                if ((flags & MethodFlags.ArgumentsInSimpleArray) > 0)
                {
                    arguments = msgInfo;
                }
                else
                {
                    int n = 0;
                    if ((flags & MethodFlags.ArgumentsInMultiArray) > 0)
                    {
                        if (msgInfo.Length > 1)
                        {
                            arguments = (object[])msgInfo[n++];
                        }
                        else
                        {
                            arguments = new object[0];
                        }
                    }

                    if ((flags & MethodFlags.GenericArguments) > 0)
                    {
                        genericArguments = (Type[])msgInfo[n++];
                    }

                    if ((flags & MethodFlags.IncludesSignature) > 0)
                    {
                        methodSignature = msgInfo[n++];
                    }

                    if ((flags & MethodFlags.IncludesLogicalCallContext) > 0)
                    {
                        callContext = msgInfo[n++];
                    }

                    if (n < msgInfo.Length)
                    {
                        extraProperties = (object[])msgInfo[n];
                    }
                }
            }
            else
            {
                reader.ReadByte(); // Reads the stream ender
            }

            if (arguments == null)
            {
                arguments = new object[0];
            }

            string uri = null;

            if (headerHandler != null)
            {
                uri = headerHandler(headers) as string;
            }

            Header[] methodInfo = new Header[7];
            methodInfo[0] = new Header("__MethodName", methodName);
            methodInfo[1] = new Header("__MethodSignature", methodSignature);
            methodInfo[2] = new Header("__TypeName", className);
            methodInfo[3] = new Header("__Args", arguments);
            methodInfo[4] = new Header("__CallContext", callContext);
            methodInfo[5] = new Header("__Uri", uri);
            methodInfo[6] = new Header("__GenericArguments", genericArguments);

            MethodCall call = new MethodCall(methodInfo);

            if (extraProperties != null)
            {
                foreach (DictionaryEntry entry in extraProperties)
                {
                    call.Properties [(string)entry.Key] = entry.Value;
                }
            }

            return(call);
        }
        public static object ReadMethodResponse(BinaryElement elem, BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, IMethodCallMessage methodCallMessage, BinaryFormatter formatter)
        {
            if (elem != BinaryElement.MethodResponse)
            {
                throw new SerializationException("Invalid format. Expected BinaryElement.MethodResponse, found " + elem);
            }
            MethodFlags   methodFlags   = (MethodFlags)reader.ReadByte();
            ReturnTypeTag returnTypeTag = (ReturnTypeTag)reader.ReadByte();
            bool          flag          = (methodFlags & MethodFlags.IncludesLogicalCallContext) > (MethodFlags)0;

            reader.ReadByte();
            reader.ReadByte();
            object ret = null;

            object[]           array   = null;
            LogicalCallContext callCtx = null;
            Exception          ex      = null;

            object[] array2  = null;
            Header[] headers = null;
            if ((byte)(returnTypeTag & ReturnTypeTag.PrimitiveType) > 0)
            {
                Type typeFromCode = BinaryCommon.GetTypeFromCode((int)reader.ReadByte());
                ret = ObjectReader.ReadPrimitiveTypeValue(reader, typeFromCode);
            }
            if ((methodFlags & MethodFlags.PrimitiveArguments) > (MethodFlags)0)
            {
                uint num = reader.ReadUInt32();
                array = new object[num];
                int num2 = 0;
                while ((long)num2 < (long)((ulong)num))
                {
                    Type typeFromCode2 = BinaryCommon.GetTypeFromCode((int)reader.ReadByte());
                    array[num2] = ObjectReader.ReadPrimitiveTypeValue(reader, typeFromCode2);
                    num2++;
                }
            }
            if (flag || (byte)(returnTypeTag & ReturnTypeTag.ObjectType) > 0 || (byte)(returnTypeTag & ReturnTypeTag.Exception) > 0 || (methodFlags & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0 || (methodFlags & MethodFlags.ArgumentsInMultiArray) > (MethodFlags)0)
            {
                ObjectReader objectReader = new ObjectReader(formatter);
                object       obj;
                objectReader.ReadObjectGraph(reader, hasHeaders, out obj, out headers);
                object[] array3 = (object[])obj;
                if ((byte)(returnTypeTag & ReturnTypeTag.Exception) > 0)
                {
                    ex = (Exception)array3[0];
                    if (flag)
                    {
                        callCtx = (LogicalCallContext)array3[1];
                    }
                }
                else if ((methodFlags & MethodFlags.NoArguments) > (MethodFlags)0 || (methodFlags & MethodFlags.PrimitiveArguments) > (MethodFlags)0)
                {
                    int num3 = 0;
                    if ((byte)(returnTypeTag & ReturnTypeTag.ObjectType) > 0)
                    {
                        ret = array3[num3++];
                    }
                    if (flag)
                    {
                        callCtx = (LogicalCallContext)array3[num3++];
                    }
                    if (num3 < array3.Length)
                    {
                        array2 = (object[])array3[num3];
                    }
                }
                else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
                {
                    array = array3;
                }
                else
                {
                    int num4 = 0;
                    array = (object[])array3[num4++];
                    if ((byte)(returnTypeTag & ReturnTypeTag.ObjectType) > 0)
                    {
                        ret = array3[num4++];
                    }
                    if (flag)
                    {
                        callCtx = (LogicalCallContext)array3[num4++];
                    }
                    if (num4 < array3.Length)
                    {
                        array2 = (object[])array3[num4];
                    }
                }
            }
            else
            {
                reader.ReadByte();
            }
            if (headerHandler != null)
            {
                headerHandler(headers);
            }
            if (ex != null)
            {
                return(new ReturnMessage(ex, methodCallMessage));
            }
            int           outArgsCount  = (array == null) ? 0 : array.Length;
            ReturnMessage returnMessage = new ReturnMessage(ret, array, outArgsCount, callCtx, methodCallMessage);

            if (array2 != null)
            {
                foreach (DictionaryEntry dictionaryEntry in array2)
                {
                    returnMessage.Properties[(string)dictionaryEntry.Key] = dictionaryEntry.Value;
                }
            }
            return(returnMessage);
        }
        public static object ReadMethodCall(BinaryElement elem, BinaryReader reader, bool hasHeaders, HeaderHandler headerHandler, BinaryFormatter formatter)
        {
            if (elem != BinaryElement.MethodCall)
            {
                throw new SerializationException("Invalid format. Expected BinaryElement.MethodCall, found " + elem);
            }
            MethodFlags methodFlags = (MethodFlags)reader.ReadInt32();

            if (reader.ReadByte() != 18)
            {
                throw new SerializationException("Invalid format");
            }
            string value = reader.ReadString();

            if (reader.ReadByte() != 18)
            {
                throw new SerializationException("Invalid format");
            }
            string value2 = reader.ReadString();

            object[] array  = null;
            object   value3 = null;
            object   value4 = null;

            object[] array2  = null;
            Header[] headers = null;
            Type[]   value5  = null;
            if ((methodFlags & MethodFlags.PrimitiveArguments) > (MethodFlags)0)
            {
                uint num = reader.ReadUInt32();
                array = new object[num];
                int num2 = 0;
                while ((long)num2 < (long)((ulong)num))
                {
                    Type typeFromCode = BinaryCommon.GetTypeFromCode((int)reader.ReadByte());
                    array[num2] = ObjectReader.ReadPrimitiveTypeValue(reader, typeFromCode);
                    num2++;
                }
            }
            if ((methodFlags & MethodFlags.NeedsInfoArrayMask) > (MethodFlags)0)
            {
                ObjectReader objectReader = new ObjectReader(formatter);
                object       obj;
                objectReader.ReadObjectGraph(reader, hasHeaders, out obj, out headers);
                object[] array3 = (object[])obj;
                if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
                {
                    array = array3;
                }
                else
                {
                    int num3 = 0;
                    if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > (MethodFlags)0)
                    {
                        if (array3.Length > 1)
                        {
                            array = (object[])array3[num3++];
                        }
                        else
                        {
                            array = new object[0];
                        }
                    }
                    if ((methodFlags & MethodFlags.GenericArguments) > (MethodFlags)0)
                    {
                        value5 = (Type[])array3[num3++];
                    }
                    if ((methodFlags & MethodFlags.IncludesSignature) > (MethodFlags)0)
                    {
                        value3 = array3[num3++];
                    }
                    if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > (MethodFlags)0)
                    {
                        value4 = array3[num3++];
                    }
                    if (num3 < array3.Length)
                    {
                        array2 = (object[])array3[num3];
                    }
                }
            }
            else
            {
                reader.ReadByte();
            }
            if (array == null)
            {
                array = new object[0];
            }
            string value6 = null;

            if (headerHandler != null)
            {
                value6 = (headerHandler(headers) as string);
            }
            MethodCall methodCall = new MethodCall(new Header[]
            {
                new Header("__MethodName", value),
                new Header("__MethodSignature", value3),
                new Header("__TypeName", value2),
                new Header("__Args", array),
                new Header("__CallContext", value4),
                new Header("__Uri", value6),
                new Header("__GenericArguments", value5)
            });

            if (array2 != null)
            {
                foreach (DictionaryEntry dictionaryEntry in array2)
                {
                    methodCall.Properties[(string)dictionaryEntry.Key] = dictionaryEntry.Value;
                }
            }
            return(methodCall);
        }