System.Runtime.Serialization.Formatters.Binary.MessageFormatter.WriteMethodResponse C# (CSharp) Method

WriteMethodResponse() public static method

public static WriteMethodResponse ( BinaryWriter writer, object obj, Header headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat ) : void
writer System.IO.BinaryWriter
obj object
headers System.Runtime.Remoting.Messaging.Header
surrogateSelector ISurrogateSelector
context System.Runtime.Serialization.StreamingContext
assemblyFormat FormatterAssemblyStyle
typeFormat FormatterTypeStyle
return void
		public static void WriteMethodResponse (BinaryWriter writer, object obj, Header[] headers, ISurrogateSelector surrogateSelector, StreamingContext context, FormatterAssemblyStyle assemblyFormat, FormatterTypeStyle typeFormat)
		{
			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 (surrogateSelector, context, assemblyFormat, typeFormat);
				objectWriter.WriteObjectGraph (writer, info, headers);
			}
			else
				writer.Write ((byte) BinaryElement.End);
		}

Usage Example

示例#1
0
        public void Serialize(Stream serializationStream, object graph, Header[] headers)
        {
            if (serializationStream == null)
            {
                throw new ArgumentNullException("serializationStream");
            }

            BinaryWriter writer = new BinaryWriter(serializationStream);

            WriteBinaryHeader(writer, headers != null);

            if (graph is IMethodCallMessage)
            {
                MessageFormatter.WriteMethodCall(writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
            }
            else if (graph is IMethodReturnMessage)
            {
                MessageFormatter.WriteMethodResponse(writer, graph, headers, surrogate_selector, context, assembly_format, type_format);
            }
            else
            {
                ObjectWriter serializer = new ObjectWriter(surrogate_selector, context, assembly_format, type_format);
                serializer.WriteObjectGraph(writer, graph, headers);
            }
            writer.Flush();
        }