Serialize() публичный Метод

public Serialize ( object obj ) : MemoryStream
obj object
Результат System.IO.MemoryStream
Пример #1
0
	        public static Data New(object data, Serializer binfmt, DataType datatype) {
				Data ret = new Data();

       			if (data == null) {
					ret.Ptr = IntPtr.Zero;
					ret.Size = 0;
				} else if (datatype == DataType.UInt) {
					staticalloc(4);
					uint value = (uint)data;
					byte[] d = staticdata;
					d[0] = (byte)((value) & 0xFF);
					d[1] = (byte)((value >> 8) & 0xFF);
					d[2] = (byte)((value >> 16) & 0xFF);
					d[3] = (byte)((value >> 24) & 0xFF);
					ret.Ptr = BytesToAlloc(d, 4, 1);
					ret.Size = (uint)4;
				} else if (datatype == DataType.IntArray) {
					int[] values = (int[])data;
					ret.Size = (uint)(4*values.Length);
					ret.Ptr = BytesToAlloc(values, values.Length, 4);
				/*} else if (datatype == DataType.String && false) {
					// Somehow this is slower than the below path.
					char[] values = ((string)data).ToCharArray();
					ret.Size = (uint)(2*values.Length);
					ret.Ptr = BytesToAlloc(values, values.Length, 2);*/
       			} else {
       				MemoryStream binary = binfmt.Serialize(data);
		       		if (binary.Length > uint.MaxValue)
		       			throw new ArgumentException("data is too large");
					ret.Ptr = BytesToAlloc(binary.GetBuffer(), (int)binary.Length, 1);
					ret.Size = (uint)binary.Length;
				}
				return ret;
			}