GetTypeCode() public static method

public static GetTypeCode ( Type type ) : byte
type System.Type
return byte
Esempio n. 1
0
        private static void EmitWriteTypeSpec(ILGenerator gen, Type type, string member)
        {
            switch (ObjectWriter.GetTypeTag(type))
            {
            case TypeTag.PrimitiveType:
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldc_I4_S, BinaryCommon.GetTypeCode(type));
                CodeGenerator.EmitWrite(gen, typeof(byte));
                break;

            case TypeTag.RuntimeType:
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldstr, type.FullName);
                CodeGenerator.EmitWrite(gen, typeof(string));
                break;

            case TypeTag.GenericType:
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldstr, type.FullName);
                CodeGenerator.EmitWrite(gen, typeof(string));
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldarg_1);
                CodeGenerator.EmitLoadTypeAssembly(gen, type, member);
                gen.EmitCall(OpCodes.Callvirt, typeof(ObjectWriter).GetMethod("GetAssemblyId"), null);
                gen.Emit(OpCodes.Conv_I4);
                CodeGenerator.EmitWrite(gen, typeof(int));
                break;

            case TypeTag.ArrayOfPrimitiveType:
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldc_I4_S, BinaryCommon.GetTypeCode(type.GetElementType()));
                CodeGenerator.EmitWrite(gen, typeof(byte));
                break;
            }
        }
Esempio n. 2
0
        public void WriteTypeSpec(BinaryWriter writer, Type type)
        {
            // WARNING Keep in sync with EmitWriteTypeSpec

            switch (GetTypeTag(type))
            {
            case TypeTag.PrimitiveType:
                writer.Write(BinaryCommon.GetTypeCode(type));
                break;

            case TypeTag.RuntimeType:
                string fullName = type.FullName;
#if NET_2_0
                // Map System.MonoType to MS.NET's System.RuntimeType,
                // when called in remoting context.
                // Note that this code does not need to be in sync with
                // EmitWriteTypeSpec because serializing a MethodCall
                // won't trigger the CodeGenerator.
                if (_context.State == StreamingContextStates.Remoting)
                {
                    if (type == typeof(System.MonoType))
                    {
                        fullName = "System.RuntimeType";
                    }
                    else if (type == typeof(System.MonoType[]))
                    {
                        fullName = "System.RuntimeType[]";
                    }
                }
#endif
                writer.Write(fullName);
                break;

            case TypeTag.GenericType:
                writer.Write(type.FullName);
                writer.Write((int)GetAssemblyId(type.Assembly));
                break;

            case TypeTag.ArrayOfPrimitiveType:
                writer.Write(BinaryCommon.GetTypeCode(type.GetElementType()));
                break;

            default:
                // Type spec not needed
                break;
            }
        }
Esempio n. 3
0
        public static void EmitWriteTypeSpec(ILGenerator gen, Type type, string member)
        {
            // WARNING Keep in sync with WriteTypeSpec

            switch (ObjectWriter.GetTypeTag(type))
            {
            case TypeTag.PrimitiveType:
                // EMIT writer.Write (BinaryCommon.GetTypeCode (type));
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldc_I4_S, (byte)BinaryCommon.GetTypeCode(type));
                EmitWrite(gen, typeof(byte));
                break;

            case TypeTag.RuntimeType:
                // EMIT writer.Write (type.FullName);
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldstr, type.FullName);
                EmitWrite(gen, typeof(string));
                break;

            case TypeTag.GenericType:
                // EMIT writer.Write (type.FullName);
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldstr, type.FullName);
                EmitWrite(gen, typeof(string));

                // EMIT writer.Write ((int)ow.GetAssemblyId (type.Assembly));
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldarg_1);
                EmitLoadTypeAssembly(gen, type, member);
                gen.EmitCall(OpCodes.Callvirt, typeof(ObjectWriter).GetMethod("GetAssemblyId"), null);
                gen.Emit(OpCodes.Conv_I4);
                EmitWrite(gen, typeof(int));
                break;

            case TypeTag.ArrayOfPrimitiveType:
                // EMIT writer.Write (BinaryCommon.GetTypeCode (type.GetElementType()));
                gen.Emit(OpCodes.Ldarg_2);
                gen.Emit(OpCodes.Ldc_I4_S, (byte)BinaryCommon.GetTypeCode(type.GetElementType()));
                EmitWrite(gen, typeof(byte));
                break;

            default:
                // Type spec not needed
                break;
            }
        }
Esempio n. 4
0
        public void WriteTypeSpec(BinaryWriter writer, Type type)
        {
            switch (ObjectWriter.GetTypeTag(type))
            {
            case TypeTag.PrimitiveType:
                writer.Write(BinaryCommon.GetTypeCode(type));
                break;

            case TypeTag.RuntimeType:
            {
                string value = type.FullName;
                if (this._context.State == StreamingContextStates.Remoting)
                {
                    if (type == typeof(MonoType))
                    {
                        value = "System.RuntimeType";
                    }
                    else if (type == typeof(MonoType[]))
                    {
                        value = "System.RuntimeType[]";
                    }
                }
                writer.Write(value);
                break;
            }

            case TypeTag.GenericType:
                writer.Write(type.FullName);
                writer.Write(this.GetAssemblyId(type.Assembly));
                break;

            case TypeTag.ArrayOfPrimitiveType:
                writer.Write(BinaryCommon.GetTypeCode(type.GetElementType()));
                break;
            }
        }
        public static void WriteMethodCall(BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter)
        {
            IMethodCallMessage call = (IMethodCallMessage)obj;

            writer.Write((byte)BinaryElement.MethodCall);

            MethodFlags methodFlags;
            int         infoArraySize = 0;
            object      info          = null;

            object[] extraProperties = null;

            if (call.LogicalCallContext != null && call.LogicalCallContext.HasInfo)
            {
                methodFlags = MethodFlags.IncludesLogicalCallContext;
                infoArraySize++;
            }
            else
            {
                methodFlags = MethodFlags.ExcludeLogicalCallContext;
            }

            if (RemotingServices.IsMethodOverloaded(call))
            {
                infoArraySize++;
                methodFlags |= MethodFlags.IncludesSignature;
            }

            if (call.Properties.Count > MethodCallDictionary.InternalKeys.Length)
            {
                extraProperties = GetExtraProperties(call.Properties, MethodCallDictionary.InternalKeys);
                infoArraySize++;
            }

            if (call.MethodBase.IsGenericMethod)
            {
                infoArraySize++;
                methodFlags |= MethodFlags.GenericArguments;
            }
            if (call.ArgCount == 0)
            {
                methodFlags |= MethodFlags.NoArguments;
            }
            else
            {
                if (AllTypesArePrimitive(call.Args))
                {
                    methodFlags |= MethodFlags.PrimitiveArguments;
                }
                else
                {
                    if (infoArraySize == 0)
                    {
                        methodFlags |= MethodFlags.ArgumentsInSimpleArray;
                    }
                    else
                    {
                        methodFlags |= MethodFlags.ArgumentsInMultiArray;
                        infoArraySize++;
                    }
                }
            }

            writer.Write((int)methodFlags);

            // Method name
            writer.Write((byte)BinaryTypeCode.String);
            writer.Write(call.MethodName);

            // Class name
            writer.Write((byte)BinaryTypeCode.String);
            writer.Write(call.TypeName);

            // Arguments

            if ((methodFlags & MethodFlags.PrimitiveArguments) > 0)
            {
                writer.Write((uint)call.Args.Length);
                for (int n = 0; n < call.ArgCount; n++)
                {
                    object arg = call.GetArg(n);
                    if (arg != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(arg.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, arg);
                    }
                    else
                    {
                        writer.Write((byte)BinaryTypeCode.Null);
                    }
                }
            }

            if (infoArraySize > 0)
            {
                object[] ainfo = new object[infoArraySize];
                int      n     = 0;
                if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > 0)
                {
                    ainfo[n++] = call.Args;
                }
                if ((methodFlags & MethodFlags.GenericArguments) > 0)
                {
                    ainfo[n++] = call.MethodBase.GetGenericArguments();
                }
                if ((methodFlags & MethodFlags.IncludesSignature) > 0)
                {
                    ainfo[n++] = call.MethodSignature;
                }
                if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > 0)
                {
                    ainfo[n++] = call.LogicalCallContext;
                }
                if (extraProperties != null)
                {
                    ainfo[n++] = extraProperties;
                }
                info = ainfo;
            }
            else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > 0)
            {
                info = call.Args;
            }

            if (info != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(formatter);
                objectWriter.WriteObjectGraph(writer, info, headers);
            }
            else
            {
                writer.Write((byte)BinaryElement.End);
            }
        }
        public static void WriteMethodResponse(BinaryWriter writer, object obj, Header[] headers, BinaryFormatter formatter)
        {
            IMethodReturnMessage resp = (IMethodReturnMessage)obj;

            writer.Write((byte)BinaryElement.MethodResponse);

            string[] internalProperties = MethodReturnDictionary.InternalReturnKeys;

            int    infoArrayLength = 0;
            object info            = null;

            object[] extraProperties = null;

            // Type of return value

            ReturnTypeTag returnTypeTag;
            MethodFlags   contextFlag = MethodFlags.ExcludeLogicalCallContext;

            if (resp.Exception != null)
            {
                returnTypeTag      = ReturnTypeTag.Exception | ReturnTypeTag.Null;
                internalProperties = MethodReturnDictionary.InternalExceptionKeys;
                infoArrayLength    = 1;
            }
            else if (resp.ReturnValue == null)
            {
                returnTypeTag = ReturnTypeTag.Null;
            }
            else if (IsMethodPrimitive(resp.ReturnValue.GetType()))
            {
                returnTypeTag = ReturnTypeTag.PrimitiveType;
            }
            else
            {
                returnTypeTag = ReturnTypeTag.ObjectType;
                infoArrayLength++;
            }

            // Message flags

            MethodFlags formatFlag;

            if ((resp.LogicalCallContext != null) && resp.LogicalCallContext.HasInfo)
            {
                contextFlag = MethodFlags.IncludesLogicalCallContext;
                infoArrayLength++;
            }

            if (resp.Properties.Count > internalProperties.Length && ((returnTypeTag & ReturnTypeTag.Exception) == 0))
            {
                extraProperties = GetExtraProperties(resp.Properties, internalProperties);
                infoArrayLength++;
            }

            if (resp.OutArgCount == 0)
            {
                formatFlag = MethodFlags.NoArguments;
            }
            else
            {
                if (AllTypesArePrimitive(resp.Args))
                {
                    formatFlag = MethodFlags.PrimitiveArguments;
                }
                else
                {
                    if (infoArrayLength == 0)
                    {
                        formatFlag = MethodFlags.ArgumentsInSimpleArray;
                    }
                    else
                    {
                        formatFlag = MethodFlags.ArgumentsInMultiArray;
                        infoArrayLength++;
                    }
                }
            }

            writer.Write((byte)(contextFlag | formatFlag));
            writer.Write((byte)returnTypeTag);

            // FIXME: what are the following 2 bytes for?
            writer.Write((byte)0);
            writer.Write((byte)0);

            // Arguments

            if (returnTypeTag == ReturnTypeTag.PrimitiveType)
            {
                writer.Write(BinaryCommon.GetTypeCode(resp.ReturnValue.GetType()));
                ObjectWriter.WritePrimitiveValue(writer, resp.ReturnValue);
            }

            if (formatFlag == MethodFlags.PrimitiveArguments)
            {
                writer.Write((uint)resp.ArgCount);
                for (int n = 0; n < resp.ArgCount; n++)
                {
                    object val = resp.GetArg(n);
                    if (val != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(val.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, val);
                    }
                    else
                    {
                        writer.Write((byte)BinaryTypeCode.Null);
                    }
                }
            }

            if (infoArrayLength > 0)
            {
                object[] infoArray = new object[infoArrayLength];
                int      n         = 0;

                if ((returnTypeTag & ReturnTypeTag.Exception) != 0)
                {
                    infoArray[n++] = resp.Exception;
                }

                if (formatFlag == MethodFlags.ArgumentsInMultiArray)
                {
                    infoArray[n++] = resp.Args;
                }

                if (returnTypeTag == ReturnTypeTag.ObjectType)
                {
                    infoArray[n++] = resp.ReturnValue;
                }

                if (contextFlag == MethodFlags.IncludesLogicalCallContext)
                {
                    infoArray[n++] = resp.LogicalCallContext;
                }

                if (extraProperties != null)
                {
                    infoArray[n++] = extraProperties;
                }

                info = infoArray;
            }
            else if ((formatFlag & MethodFlags.ArgumentsInSimpleArray) > 0)
            {
                info = resp.Args;
            }

            if (info != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(formatter);
                objectWriter.WriteObjectGraph(writer, info, headers);
            }
            else
            {
                writer.Write((byte)BinaryElement.End);
            }
        }
        public static void WriteMethodResponse(BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
        {
            IMethodReturnMessage methodReturnMessage = (IMethodReturnMessage)obj;

            writer.Write(22);
            string[] array = MethodReturnDictionary.InternalReturnKeys;
            int      num   = 0;
            object   obj2  = null;

            object[]      array2      = null;
            MethodFlags   methodFlags = MethodFlags.ExcludeLogicalCallContext;
            ReturnTypeTag returnTypeTag;

            if (methodReturnMessage.Exception != null)
            {
                returnTypeTag = (ReturnTypeTag)34;
                array         = MethodReturnDictionary.InternalExceptionKeys;
                num           = 1;
            }
            else if (methodReturnMessage.ReturnValue == null)
            {
                returnTypeTag = ReturnTypeTag.Null;
            }
            else if (MessageFormatter.IsMethodPrimitive(methodReturnMessage.ReturnValue.GetType()))
            {
                returnTypeTag = ReturnTypeTag.PrimitiveType;
            }
            else
            {
                returnTypeTag = ReturnTypeTag.ObjectType;
                num++;
            }
            if (methodReturnMessage.LogicalCallContext != null && methodReturnMessage.LogicalCallContext.HasInfo)
            {
                methodFlags = MethodFlags.IncludesLogicalCallContext;
                num++;
            }
            if (methodReturnMessage.Properties.Count > array.Length && (byte)(returnTypeTag & ReturnTypeTag.Exception) == 0)
            {
                array2 = MessageFormatter.GetExtraProperties(methodReturnMessage.Properties, array);
                num++;
            }
            MethodFlags methodFlags2;

            if (methodReturnMessage.OutArgCount == 0)
            {
                methodFlags2 = MethodFlags.NoArguments;
            }
            else if (MessageFormatter.AllTypesArePrimitive(methodReturnMessage.Args))
            {
                methodFlags2 = MethodFlags.PrimitiveArguments;
            }
            else if (num == 0)
            {
                methodFlags2 = MethodFlags.ArgumentsInSimpleArray;
            }
            else
            {
                methodFlags2 = MethodFlags.ArgumentsInMultiArray;
                num++;
            }
            writer.Write((byte)(methodFlags | methodFlags2));
            writer.Write((byte)returnTypeTag);
            writer.Write(0);
            writer.Write(0);
            if (returnTypeTag == ReturnTypeTag.PrimitiveType)
            {
                writer.Write(BinaryCommon.GetTypeCode(methodReturnMessage.ReturnValue.GetType()));
                ObjectWriter.WritePrimitiveValue(writer, methodReturnMessage.ReturnValue);
            }
            if (methodFlags2 == MethodFlags.PrimitiveArguments)
            {
                writer.Write((uint)methodReturnMessage.ArgCount);
                for (int i = 0; i < methodReturnMessage.ArgCount; i++)
                {
                    object arg = methodReturnMessage.GetArg(i);
                    if (arg != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(arg.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, arg);
                    }
                    else
                    {
                        writer.Write(17);
                    }
                }
            }
            if (num > 0)
            {
                object[] array3 = new object[num];
                int      num2   = 0;
                if ((byte)(returnTypeTag & ReturnTypeTag.Exception) != 0)
                {
                    array3[num2++] = methodReturnMessage.Exception;
                }
                if (methodFlags2 == MethodFlags.ArgumentsInMultiArray)
                {
                    array3[num2++] = methodReturnMessage.Args;
                }
                if (returnTypeTag == ReturnTypeTag.ObjectType)
                {
                    array3[num2++] = methodReturnMessage.ReturnValue;
                }
                if (methodFlags == MethodFlags.IncludesLogicalCallContext)
                {
                    array3[num2++] = methodReturnMessage.LogicalCallContext;
                }
                if (array2 != null)
                {
                    array3[num2++] = array2;
                }
                obj2 = array3;
            }
            else if ((methodFlags2 & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
            {
                obj2 = methodReturnMessage.Args;
            }
            if (obj2 != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context, assemblyFormat, typeFormat);
                objectWriter.WriteObjectGraph(writer, obj2, headers);
            }
            else
            {
                writer.Write(11);
            }
        }
        public static void WriteMethodCall(BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
        {
            IMethodCallMessage methodCallMessage = (IMethodCallMessage)obj;

            writer.Write(21);
            int    num  = 0;
            object obj2 = null;

            object[]    array = null;
            MethodFlags methodFlags;

            if (methodCallMessage.LogicalCallContext != null && methodCallMessage.LogicalCallContext.HasInfo)
            {
                methodFlags = MethodFlags.IncludesLogicalCallContext;
                num++;
            }
            else
            {
                methodFlags = MethodFlags.ExcludeLogicalCallContext;
            }
            if (RemotingServices.IsMethodOverloaded(methodCallMessage))
            {
                num++;
                methodFlags |= MethodFlags.IncludesSignature;
            }
            if (methodCallMessage.Properties.Count > MethodCallDictionary.InternalKeys.Length)
            {
                array = MessageFormatter.GetExtraProperties(methodCallMessage.Properties, MethodCallDictionary.InternalKeys);
                num++;
            }
            if (methodCallMessage.MethodBase.IsGenericMethod)
            {
                num++;
                methodFlags |= MethodFlags.GenericArguments;
            }
            if (methodCallMessage.ArgCount == 0)
            {
                methodFlags |= MethodFlags.NoArguments;
            }
            else if (MessageFormatter.AllTypesArePrimitive(methodCallMessage.Args))
            {
                methodFlags |= MethodFlags.PrimitiveArguments;
            }
            else if (num == 0)
            {
                methodFlags |= MethodFlags.ArgumentsInSimpleArray;
            }
            else
            {
                methodFlags |= MethodFlags.ArgumentsInMultiArray;
                num++;
            }
            writer.Write((int)methodFlags);
            writer.Write(18);
            writer.Write(methodCallMessage.MethodName);
            writer.Write(18);
            writer.Write(methodCallMessage.TypeName);
            if ((methodFlags & MethodFlags.PrimitiveArguments) > (MethodFlags)0)
            {
                writer.Write((uint)methodCallMessage.Args.Length);
                for (int i = 0; i < methodCallMessage.ArgCount; i++)
                {
                    object arg = methodCallMessage.GetArg(i);
                    if (arg != null)
                    {
                        writer.Write(BinaryCommon.GetTypeCode(arg.GetType()));
                        ObjectWriter.WritePrimitiveValue(writer, arg);
                    }
                    else
                    {
                        writer.Write(17);
                    }
                }
            }
            if (num > 0)
            {
                object[] array2 = new object[num];
                int      num2   = 0;
                if ((methodFlags & MethodFlags.ArgumentsInMultiArray) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.Args;
                }
                if ((methodFlags & MethodFlags.GenericArguments) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.MethodBase.GetGenericArguments();
                }
                if ((methodFlags & MethodFlags.IncludesSignature) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.MethodSignature;
                }
                if ((methodFlags & MethodFlags.IncludesLogicalCallContext) > (MethodFlags)0)
                {
                    array2[num2++] = methodCallMessage.LogicalCallContext;
                }
                if (array != null)
                {
                    array2[num2++] = array;
                }
                obj2 = array2;
            }
            else if ((methodFlags & MethodFlags.ArgumentsInSimpleArray) > (MethodFlags)0)
            {
                obj2 = methodCallMessage.Args;
            }
            if (obj2 != null)
            {
                ObjectWriter objectWriter = new ObjectWriter(surrogateSelector, context, assemblyFormat, typeFormat);
                objectWriter.WriteObjectGraph(writer, obj2, headers);
            }
            else
            {
                writer.Write(11);
            }
        }