private static void EmitReadFields(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 = SerializeReaderHelper <T, R> .FindDefinedDeserializeMethod(fieldType); if (methodInfo == null) { methodInfo = typeof(SerializeReaderHelper <, >).MakeGenericType(new Type[] { fieldType, typeof(R) }).GetMethod("Deserialize"); } il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); if (!type.IsValueType) { il.Emit(OpCodes.Ldind_Ref); } il.Emit(OpCodes.Ldflda, fieldInfo); il.EmitCall(OpCodes.Call, methodInfo, null); } } }
private static void Validate(Type type) { if (type == null) { throw new ArgumentNullException("type"); } if (type.IsArray) { SerializeReaderHelper <T, R> .Validate(type.GetElementType()); return; } if (type.IsGenericType) { Type[] genericArguments = type.GetGenericArguments(); int i = 0; int num = genericArguments.Length; while (i < num) { SerializeReaderHelper <T, R> .Validate(genericArguments[i]); i++; } return; } if (type.GetType() == typeof(SerializeReader.UnknownType)) { throw new InvalidOperationException(string.Format("UnknownType {{ GUID = {0} }}", type.GUID)); } }
private static void DeserializeVirtual(ref R reader, out T value) { Type type; SerializeReaderHelper <Type, R> .Deserialize(ref reader, out type); SerializeReaderHelper <T, R> .Validate(type); if (SerializeReaderHelper <T, R> .deserializers == null) { SerializeReaderHelper <T, R> .deserializers = new Dictionary <Type, SerializeReaderHelper <T, R> .DeserializeDelegate>(); } SerializeReaderHelper <T, R> .DeserializeDelegate deserializeDelegate; if (!SerializeReaderHelper <T, R> .deserializers.TryGetValue(type, out deserializeDelegate)) { MethodInfo method = typeof(SerializeReaderHelper <, >).MakeGenericType(new Type[] { type, typeof(R) }).GetMethod("DeserializeCore", BindingFlags.Static | BindingFlags.NonPublic); DynamicMethod dynamicMethod = new DynamicMethod(string.Format("{0}.{1}.DeserializeAs[{2}.{3}]", new object[] { typeof(SerializeReaderHelper <T, R>).Namespace, typeof(SerializeReaderHelper <T, R>).Name, typeof(T).Namespace, typeof(T).Name }), null, new Type[] { typeof(R).MakeByRefType(), typeof(T).MakeByRefType() }, typeof(SerializeReaderHelper <T, R>), true); dynamicMethod.DefineParameter(1, ParameterAttributes.In | ParameterAttributes.Out, "reader"); dynamicMethod.DefineParameter(2, ParameterAttributes.Out, "value"); ILGenerator ilgenerator = dynamicMethod.GetILGenerator(); ilgenerator.Emit(OpCodes.Ldarg_0); LocalBuilder local = ilgenerator.DeclareLocal(type); ilgenerator.Emit(OpCodes.Ldloca_S, local); ilgenerator.EmitCall(OpCodes.Call, method, null); ilgenerator.Emit(OpCodes.Ldarg_1); ilgenerator.Emit(OpCodes.Ldloc, local); if (type.IsValueType) { ilgenerator.Emit(OpCodes.Box, type); } ilgenerator.Emit(OpCodes.Stind_Ref); ilgenerator.Emit(OpCodes.Ret); deserializeDelegate = (dynamicMethod.CreateDelegate(typeof(SerializeReaderHelper <T, R> .DeserializeDelegate)) as SerializeReaderHelper <T, R> .DeserializeDelegate); SerializeReaderHelper <T, R> .deserializers.Add(type, deserializeDelegate); } deserializeDelegate(ref reader, out value); }
private void Read <T>(out T?value) where T : struct { bool flag; this.Read(out flag); if (!flag) { value = null; return; } T value2; SerializeReaderHelper <T, SerializeReader> .Deserialize(ref this, out value2); value = new T?(value2); }
private void Read <T>(out T[] value) { int num; this.ReadCount(out num); if (num == 0) { value = null; return; } value = new T[--num]; for (int i = 0; i < num; i++) { SerializeReaderHelper <T, SerializeReader> .Deserialize(ref this, out value[i]); } }
private void Read(out IDictionary values) { int num; this.ReadCount(out num); if (num == 0) { values = null; return; } values = new Hashtable(--num); for (int i = 0; i < num; i++) { DictionaryEntry dictionaryEntry; SerializeReaderHelper <DictionaryEntry, SerializeReader> .Deserialize(ref this, out dictionaryEntry); values.Add(dictionaryEntry.Key, dictionaryEntry.Value); } }
public static void FromBinary <T>(Packet packet, out T value) { int categoryId = ClassInfo <T> .CategoryId; SerializeReader serializeReader = new SerializeReader(packet); if (packet.CategoryId == 0) { object obj; SerializeReaderHelper <object, SerializeReader> .Deserialize(ref serializeReader, out obj); value = (T)((object)obj); return; } if (packet.CategoryId != categoryId && typeof(T).IsSealed) { throw new SerializationException(string.Format("Unexpected category {0:X8} for type {1}", packet.CategoryId, typeof(T).AssemblyQualifiedName)); } SerializeReaderHelper <T, SerializeReader> .Deserialize(ref serializeReader, out value); }
private void Read <T, TCollection>(out TCollection values) where TCollection : ICollection <T>, new() { int num; this.ReadCount(out num); if (num == 0) { values = default(TCollection); return; } num--; values = ((default(TCollection) == null) ? Activator.CreateInstance <TCollection>() : default(TCollection)); for (int i = 0; i < num; i++) { T item; SerializeReaderHelper <T, SerializeReader> .Deserialize(ref this, out item); values.Add(item); } }
private void Read <TKey, TValue>(out IDictionary <TKey, TValue> values) { int num; this.ReadCount(out num); if (num == 0) { values = null; return; } num--; values = new Dictionary <TKey, TValue>(num); for (int i = 0; i < num; i++) { KeyValuePair <TKey, TValue> item; SerializeReaderHelper <KeyValuePair <TKey, TValue>, SerializeReader> .Deserialize(ref this, out item); values.Add(item); } }
private void Read(out Type value) { Guid guid; SerializeReaderHelper <Guid, SerializeReader> .Deserialize(ref this, out guid); if (guid == Guid.Empty) { value = null; return; } if (guid == SerializeReader.arrayGuid) { Type type; this.Read(out type); int num; this.ReadCount(out num); value = ((num == 0) ? type.MakeArrayType() : type.MakeArrayType(num)); return; } if (SerializeReader.dictionary == null) { SerializeReader.dictionary = new Dictionary <Guid, Type>(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { try { foreach (Type type2 in assembly.GetTypes()) { if ((!(type2.Namespace != "System") || !(type2.Namespace != "System.Collections.Generic")) && type2.IsSerializable && !type2.IsDefined(typeof(ObsoleteAttribute), false) && !type2.IsInterface && !type2.IsAbstract && !type2.IsArray && type2.IsVisible) { SerializeReader.dictionary.Add(type2.GUID, type2); } } } catch (ReflectionTypeLoadException) { } } } if (!SerializeReader.dictionary.TryGetValue(guid, out value)) { foreach (Assembly assembly2 in AppDomain.CurrentDomain.GetAssemblies()) { try { foreach (Type type3 in assembly2.GetTypes()) { if (!(type3.GUID != guid) && type3.IsSerializable && !type3.IsDefined(typeof(ObsoleteAttribute), false) && (type3.IsVisible || !type3.FullName.StartsWith("System."))) { value = type3; break; } } if (!(value == null)) { break; } } catch (ReflectionTypeLoadException) { } } if (value == null) { value = new SerializeReader.UnknownType(guid); return; } foreach (Assembly assembly3 in AppDomain.CurrentDomain.GetAssemblies()) { try { foreach (Type type4 in assembly3.GetTypes()) { if (!(type4.Namespace != value.Namespace) && type4.IsSerializable && !type4.IsDefined(typeof(ObsoleteAttribute), false) && !type4.IsInterface && !type4.IsAbstract && !type4.IsArray && type4.IsVisible) { if (SerializeReader.dictionary.ContainsKey(type4.GUID)) { SerializeReader.dictionary[type4.GUID] = null; } else { SerializeReader.dictionary.Add(type4.GUID, type4); } } } } catch (ReflectionTypeLoadException) { } } } if (value == null) { value = new SerializeReader.UnknownType(guid); return; } if (value.IsGenericTypeDefinition) { Type[] array = new Type[value.GetGenericArguments().Length]; for (int num2 = 0; num2 < array.Length; num2++) { this.Read(out array[num2]); } value = value.MakeGenericType(array); } }
private static void GenerateDeserializeMethod() { object[] @namespace = new object[] { typeof(SerializeReaderHelper <T, R>).Namespace, typeof(SerializeReaderHelper <T, R>).Name, typeof(T).Namespace, typeof(T).Name }; string str = string.Format("{0}.{1}.DeserializeCore[{2}.{3}]", @namespace); Type[] typeArray = new Type[] { typeof(R).MakeByRefType(), typeof(T).MakeByRefType() }; DynamicMethod dynamicMethod = new DynamicMethod(str, null, typeArray, typeof(SerializeReaderHelper <T, R>), true); dynamicMethod.DefineParameter(1, ParameterAttributes.In | ParameterAttributes.Out, "reader"); dynamicMethod.DefineParameter(2, ParameterAttributes.Out, "value"); ILGenerator lGenerator = dynamicMethod.GetILGenerator(); Type baseType = typeof(T); if (!baseType.IsSerializable && SerializeReaderHelper <T, R> .FindDefinedDeserializeMethod(baseType) == null) { throw new SerializationException(string.Format("Type is not serializable: {0}", baseType.AssemblyQualifiedName)); } Label?nullable = null; if (baseType.IsValueType) { lGenerator.Emit(OpCodes.Ldarg_1); lGenerator.Emit(OpCodes.Initobj, baseType); } else if (SerializeReaderHelper <T, R> .FindDefinedDeserializeMethod(baseType) == null) { MethodInfo method = SerializeReaderHelper <T, R> .FindDefinedDeserializeMethod(typeof(bool)); if (method == null) { Type type = typeof(SerializeReaderHelper <,>); Type[] typeArray1 = new Type[] { typeof(bool), typeof(R) }; method = type.MakeGenericType(typeArray1).GetMethod("Deserialize"); } lGenerator.Emit(OpCodes.Ldarg_0); LocalBuilder localBuilder = lGenerator.DeclareLocal(typeof(bool)); lGenerator.Emit(OpCodes.Ldloca_S, localBuilder); lGenerator.EmitCall(OpCodes.Call, method, null); lGenerator.Emit(OpCodes.Ldloc, localBuilder); nullable = new Label?(lGenerator.DefineLabel()); lGenerator.Emit(OpCodes.Brfalse, nullable.Value); if (baseType.IsInterface || baseType.IsAbstract) { lGenerator.Emit(OpCodes.Ldstr, string.Format("Type cannot be properly initialized: {0}", baseType.AssemblyQualifiedName)); Type type1 = typeof(SerializationException); Type[] typeArray2 = new Type[] { typeof(string) }; ConstructorInfo constructor = type1.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, typeArray2, null); lGenerator.Emit(OpCodes.Newobj, constructor); lGenerator.Emit(OpCodes.Throw); } else { lGenerator.Emit(OpCodes.Ldarg_1); ConstructorInfo constructorInfo = baseType.GetConstructor(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); if (constructorInfo == null) { MethodInfo methodInfo = typeof(RuntimeTypeHandle).GetMethod("Allocate", BindingFlags.Static | BindingFlags.NonPublic); MethodInfo method1 = typeof(Type).GetMethod("GetTypeFromHandle", BindingFlags.Static | BindingFlags.Public); lGenerator.Emit(OpCodes.Ldtoken, baseType); lGenerator.Emit(OpCodes.Call, method1); lGenerator.EmitCall(OpCodes.Call, methodInfo, null); } else { lGenerator.Emit(OpCodes.Newobj, constructorInfo); } lGenerator.Emit(OpCodes.Stind_Ref); } } Stack <Type> types = new Stack <Type>(); while (true) { if (baseType != null) { MethodInfo methodInfo1 = SerializeReaderHelper <T, R> .FindDefinedDeserializeMethod(baseType); if (methodInfo1 == null) { if (baseType.IsSerializable) { types.Push(baseType); } baseType = baseType.BaseType; } else { SerializeReaderHelper <T, R> .EmitReadPredefinedType(lGenerator, methodInfo1); break; } } else { break; } } while (0 < types.Count) { SerializeReaderHelper <T, R> .EmitReadFields(lGenerator, types.Pop()); } if (nullable.HasValue) { lGenerator.MarkLabel(nullable.Value); } lGenerator.Emit(OpCodes.Ret); SerializeReaderHelper <T, R> .deserializeCore = dynamicMethod.CreateDelegate(typeof(SerializeReaderHelper <T, R> .DeserializeDelegate)) as SerializeReaderHelper <T, R> .DeserializeDelegate; if (typeof(T).IsSealed || SerializeReaderHelper <T, R> .FindDefinedDeserializeMethod(typeof(T)) != null) { SerializeReaderHelper <T, R> .deserialize = SerializeReaderHelper <T, R> .deserializeCore; return; } SerializeReaderHelper <T, R> .deserialize = new SerializeReaderHelper <T, R> .DeserializeDelegate(SerializeReaderHelper <T, R> .DeserializeVirtual); }
private static void DeserializeCore(ref R reader, out T value) { SerializeReaderHelper <T, R> .deserializeCore(ref reader, out value); }
static SerializeReaderHelper() { SerializeReaderHelper <T, R> .GenerateDeserializeMethod(); }
public static void Deserialize(ref R reader, out T value) { SerializeReaderHelper <T, R> .deserialize(ref reader, out value); }