private void Write(Type value) { if (value == null) { SerializeWriterHelper <Guid, SerializeWriter> .Serialize(ref this, Guid.Empty); return; } if (value.IsArray) { SerializeWriterHelper <Guid, SerializeWriter> .Serialize(ref this, SerializeWriter.arrayGuid); this.Write(value.GetElementType()); this.WriteCount(value.Name.EndsWith("[]") ? 0 : value.GetArrayRank()); return; } if (value.IsGenericType) { SerializeWriterHelper <Guid, SerializeWriter> .Serialize(ref this, value.GetGenericTypeDefinition().GUID); Type[] genericArguments = value.GetGenericArguments(); int i = 0; int num = genericArguments.Length; while (i < num) { this.Write(genericArguments[i]); i++; } return; } SerializeWriterHelper <Guid, SerializeWriter> .Serialize(ref this, value.GUID); }
private void Write <T>(T?value) where T : struct { this.Write(value != null); if (value != null) { SerializeWriterHelper <T, SerializeWriter> .Serialize(ref this, value.Value); } }
public static Packet ToBinary(object value) { int length = SerializedSizeCalculator.Calculate <object>(value); SerializeWriter serializeWriter = new SerializeWriter(length, 0); SerializeWriterHelper <object, SerializeWriter> .Serialize(ref serializeWriter, value); return(serializeWriter.data); }
private void Write <T>(ICollection <T> values) { if (values == null) { this.WriteCount(0); return; } this.WriteCount(values.Count + 1); foreach (T value in values) { SerializeWriterHelper <T, SerializeWriter> .Serialize(ref this, value); } }
public static Packet ToBinary <T>(T value) { int categoryId = ClassInfo <T> .CategoryId; if (categoryId == 0) { return(SerializeWriter.ToBinary(value)); } int length = SerializedSizeCalculator.Calculate <T>(value); SerializeWriter serializeWriter = new SerializeWriter(length, categoryId); SerializeWriterHelper <T, SerializeWriter> .Serialize(ref serializeWriter, value); return(serializeWriter.data); }
private void Write <T>(ArraySegment <T> value) { if (value.Array == null) { this.WriteCount(0); return; } this.WriteCount(value.Count + 1); int i = value.Offset; int num = value.Offset + value.Count; while (i < num) { SerializeWriterHelper <T, SerializeWriter> .Serialize(ref this, value.Array[i]); i++; } }
private void Write <T>(IList <T> value) { if (value == null) { this.WriteCount(0); return; } this.WriteCount(value.Count + 1); int i = 0; int count = value.Count; while (i < count) { SerializeWriterHelper <T, SerializeWriter> .Serialize(ref this, value[i]); i++; } }
private static void SerializeCoreVirtual(ref W writer, T value) { Type type = (value == null) ? typeof(T) : value.GetType(); SerializeWriterHelper <Type, W> .Serialize(ref writer, type); if (SerializeWriterHelper <T, W> .serializers == null) { SerializeWriterHelper <T, W> .serializers = new Dictionary <Type, SerializeWriterHelper <T, W> .SerializeDelegate>(); } SerializeWriterHelper <T, W> .SerializeDelegate serializeDelegate; if (!SerializeWriterHelper <T, W> .serializers.TryGetValue(type, out serializeDelegate)) { MethodInfo method = typeof(SerializeWriterHelper <, >).MakeGenericType(new Type[] { type, typeof(W) }).GetMethod("SerializeCore", BindingFlags.Static | BindingFlags.NonPublic); DynamicMethod dynamicMethod = new DynamicMethod(string.Format("{0}.{1}.SerializeAs[{2}.{3}]", new object[] { typeof(SerializeWriterHelper <Type, W>).Namespace, typeof(SerializeWriterHelper <Type, W>).Name, typeof(T).Namespace, typeof(T) }), null, new Type[] { typeof(W).MakeByRefType(), typeof(T) }, typeof(SerializeWriterHelper <Type, W>), true); dynamicMethod.DefineParameter(1, ParameterAttributes.In | ParameterAttributes.Out, "writer"); dynamicMethod.DefineParameter(2, ParameterAttributes.In, "value"); ILGenerator ilgenerator = dynamicMethod.GetILGenerator(); ilgenerator.Emit(OpCodes.Ldarg_0); ilgenerator.Emit(OpCodes.Ldarg_1); ilgenerator.Emit(OpCodes.Unbox_Any, method.GetParameters()[1].ParameterType); ilgenerator.EmitCall(OpCodes.Call, method, null); ilgenerator.Emit(OpCodes.Ret); serializeDelegate = (dynamicMethod.CreateDelegate(typeof(SerializeWriterHelper <T, W> .SerializeDelegate)) as SerializeWriterHelper <T, W> .SerializeDelegate); SerializeWriterHelper <T, W> .serializers.Add(type, serializeDelegate); } serializeDelegate(ref writer, value); }
private static void EmitWriteFields(ILGenerator il, Type type) { foreach (FieldInfo fieldInfo in type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (!fieldInfo.IsInitOnly && !fieldInfo.IsLiteral && !fieldInfo.IsNotSerialized) { Type fieldType = fieldInfo.FieldType; MethodInfo methodInfo = SerializeWriterHelper <T, W> .FindDefinedSerializeMethod(fieldType); if (methodInfo == null) { methodInfo = typeof(SerializeWriterHelper <, >).MakeGenericType(new Type[] { fieldType, typeof(W) }).GetMethod("Serialize"); } il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ldfld, fieldInfo); il.EmitCall(OpCodes.Call, methodInfo, null); } } }
public static void Serialize(ref W writer, T value) { SerializeWriterHelper <T, W> .serialize(ref writer, value); }
private static void GenerateSerializeMethod() { object[] @namespace = new object[] { typeof(SerializeWriterHelper <Type, W>).Namespace, typeof(SerializeWriterHelper <Type, W>).Name, typeof(T).Namespace, typeof(T).Name }; string str = string.Format("{0}.{1}.SerializeCore[{2}.{3}]", @namespace); Type[] typeArray = new Type[] { typeof(W).MakeByRefType(), typeof(T) }; DynamicMethod dynamicMethod = new DynamicMethod(str, null, typeArray, typeof(SerializeWriterHelper <T, W>), true); dynamicMethod.DefineParameter(1, ParameterAttributes.In | ParameterAttributes.Out, "writer"); dynamicMethod.DefineParameter(2, ParameterAttributes.In, "value"); ILGenerator lGenerator = dynamicMethod.GetILGenerator(); Type baseType = typeof(T); if (!baseType.IsSerializable && SerializeWriterHelper <T, W> .FindDefinedSerializeMethod(baseType) == null) { throw new SerializationException(string.Format("Type is not serializable: {0}", baseType.AssemblyQualifiedName)); } Label?nullable = null; if (!baseType.IsValueType && SerializeWriterHelper <T, W> .FindDefinedSerializeMethod(baseType) == null) { MethodInfo method = SerializeWriterHelper <T, W> .FindDefinedSerializeMethod(typeof(bool)); if (method == null) { Type type = typeof(SerializeWriterHelper <,>); Type[] typeArray1 = new Type[] { typeof(bool), typeof(W) }; method = type.MakeGenericType(typeArray1).GetMethod("Serialize"); } lGenerator.Emit(OpCodes.Ldarg_0); lGenerator.Emit(OpCodes.Ldarg_1); lGenerator.Emit(OpCodes.Ldnull); lGenerator.Emit(OpCodes.Ceq); lGenerator.Emit(OpCodes.Ldc_I4_0); lGenerator.Emit(OpCodes.Ceq); lGenerator.EmitCall(OpCodes.Call, method, null); lGenerator.Emit(OpCodes.Ldarg_1); nullable = new Label?(lGenerator.DefineLabel()); lGenerator.Emit(OpCodes.Brfalse, nullable.Value); } Stack <Type> types = new Stack <Type>(); while (true) { if (baseType != null) { MethodInfo methodInfo = SerializeWriterHelper <T, W> .FindDefinedSerializeMethod(baseType); if (methodInfo == null) { if (baseType.IsSerializable) { types.Push(baseType); } baseType = baseType.BaseType; } else { SerializeWriterHelper <T, W> .EmitWritePredefinedType(lGenerator, methodInfo); break; } } else { break; } } while (0 < types.Count) { SerializeWriterHelper <T, W> .EmitWriteFields(lGenerator, types.Pop()); } if (nullable.HasValue) { lGenerator.MarkLabel(nullable.Value); } lGenerator.Emit(OpCodes.Ret); SerializeWriterHelper <T, W> .serializeCore = dynamicMethod.CreateDelegate(typeof(SerializeWriterHelper <T, W> .SerializeDelegate)) as SerializeWriterHelper <T, W> .SerializeDelegate; if (typeof(T).IsSealed || SerializeWriterHelper <T, W> .FindDefinedSerializeMethod(typeof(T)) != null) { SerializeWriterHelper <T, W> .serialize = SerializeWriterHelper <T, W> .serializeCore; return; } SerializeWriterHelper <T, W> .serialize = new SerializeWriterHelper <T, W> .SerializeDelegate(SerializeWriterHelper <T, W> .SerializeCoreVirtual); }
private static void SerializeCore(ref W writer, T value) { SerializeWriterHelper <T, W> .serializeCore(ref writer, value); }
static SerializeWriterHelper() { SerializeWriterHelper <T, W> .GenerateSerializeMethod(); }