protected override void InitUnserializeDelegate(Type type, string[] names, DynamicMethod dynamicMethod) { Dictionary <string, MemberInfo> members = HproseHelper.GetMembers(type); ILGenerator gen = dynamicMethod.GetILGenerator(); LocalBuilder e = gen.DeclareLocal(typeofException); int count = names.Length; for (int i = 0; i < count; i++) { Label exTryCatch = gen.BeginExceptionBlock(); if (type.IsValueType) { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Unbox, type); } else { gen.Emit(OpCodes.Ldarg_0); } gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4, i); gen.Emit(OpCodes.Ldelem_Ref); if (members[names[i]] is FieldInfo) { FieldInfo fieldInfo = (FieldInfo)members[names[i]]; if (fieldInfo.FieldType.IsValueType) { gen.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType); } gen.Emit(OpCodes.Stfld, fieldInfo); } else { PropertyInfo propertyInfo = (PropertyInfo)members[names[i]]; if (propertyInfo.PropertyType.IsValueType) { gen.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType); } MethodInfo setMethod = propertyInfo.GetSetMethod(); if (setMethod.IsVirtual) { gen.Emit(OpCodes.Callvirt, setMethod); } else { gen.Emit(OpCodes.Call, setMethod); } } gen.Emit(OpCodes.Leave_S, exTryCatch); gen.BeginCatchBlock(typeofException); gen.Emit(OpCodes.Stloc_S, e); gen.Emit(OpCodes.Ldstr, "The member value can\'t be unserialized."); gen.Emit(OpCodes.Ldloc_S, e); gen.Emit(OpCodes.Newobj, hproseExceptionCtor); gen.Emit(OpCodes.Throw); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.EndExceptionBlock(); } gen.Emit(OpCodes.Ret); }
private void InitSerializePropertiesDelegate(Type type) { Dictionary <string, MemberInfo> properties = HproseHelper.GetProperties(type); DynamicMethod dynamicMethod = new DynamicMethod("$SerializeProperties", typeofVoid, typeofArgs, type, true); ILGenerator gen = dynamicMethod.GetILGenerator(); LocalBuilder value = gen.DeclareLocal(typeofObject); LocalBuilder e = gen.DeclareLocal(typeofException); foreach (KeyValuePair <string, MemberInfo> property in properties) { PropertyInfo propertyInfo = (PropertyInfo)property.Value; Label exTryCatch = gen.BeginExceptionBlock(); if (type.IsValueType) { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Unbox, type); } else { gen.Emit(OpCodes.Ldarg_0); } MethodInfo getMethod = propertyInfo.GetGetMethod(); if (getMethod.IsVirtual) { gen.Emit(OpCodes.Callvirt, getMethod); } else { gen.Emit(OpCodes.Call, getMethod); } if (propertyInfo.PropertyType.IsValueType) { gen.Emit(OpCodes.Box, propertyInfo.PropertyType); } gen.Emit(OpCodes.Stloc_S, value); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.BeginCatchBlock(typeofException); gen.Emit(OpCodes.Stloc_S, e); gen.Emit(OpCodes.Ldstr, "The property value can\'t be serialized."); gen.Emit(OpCodes.Ldloc_S, e); gen.Emit(OpCodes.Newobj, hproseExceptionCtor); gen.Emit(OpCodes.Throw); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.EndExceptionBlock(); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldloc_S, value); gen.Emit(OpCodes.Call, serializeMethod); } gen.Emit(OpCodes.Ret); serializePropertiesDelegate = (SerializeDelegate)dynamicMethod.CreateDelegate(typeofSerializeDelegate); }
protected override void InitUnserializeDelegate(Type type, string[] names, DynamicMethod dynamicMethod) { Dictionary <string, PropertyTypeInfo> properties = HproseHelper.GetProperties(type); ILGenerator gen = dynamicMethod.GetILGenerator(); LocalBuilder e = gen.DeclareLocal(typeofException); int count = names.Length; for (int i = 0; i < count; ++i) { PropertyTypeInfo property; if (properties.TryGetValue(names[i], out property)) { Label exTryCatch = gen.BeginExceptionBlock(); if (type.IsValueType) { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Unbox, type); } else { gen.Emit(OpCodes.Ldarg_0); } gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4, i); gen.Emit(OpCodes.Ldelem_Ref); if (property.type.IsValueType) { gen.Emit(OpCodes.Unbox_Any, property.type); } MethodInfo setMethod = property.info.GetSetMethod(true); if (setMethod.IsVirtual) { gen.Emit(OpCodes.Callvirt, setMethod); } else { gen.Emit(OpCodes.Call, setMethod); } gen.Emit(OpCodes.Leave_S, exTryCatch); gen.BeginCatchBlock(typeofException); gen.Emit(OpCodes.Stloc_S, e); gen.Emit(OpCodes.Ldstr, "The property value can\'t be unserialized."); gen.Emit(OpCodes.Ldloc_S, e); gen.Emit(OpCodes.Newobj, hproseExceptionCtor); gen.Emit(OpCodes.Throw); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.EndExceptionBlock(); } } gen.Emit(OpCodes.Ret); }
private void InitSerializeFieldsDelegate(Type type) { Dictionary <string, MemberInfo> fields = HproseHelper.GetFields(type); DynamicMethod dynamicMethod = new DynamicMethod("$SerializeFields", typeofVoid, typeofArgs, type, true); ILGenerator gen = dynamicMethod.GetILGenerator(); LocalBuilder value = gen.DeclareLocal(typeofObject); LocalBuilder e = gen.DeclareLocal(typeofException); foreach (KeyValuePair <string, MemberInfo> field in fields) { FieldInfo fieldInfo = (FieldInfo)field.Value; Label exTryCatch = gen.BeginExceptionBlock(); if (type.IsValueType) { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Unbox, type); } else { gen.Emit(OpCodes.Ldarg_0); } gen.Emit(OpCodes.Ldfld, fieldInfo); if (fieldInfo.FieldType.IsValueType) { gen.Emit(OpCodes.Box, fieldInfo.FieldType); } gen.Emit(OpCodes.Stloc_S, value); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.BeginCatchBlock(typeofException); gen.Emit(OpCodes.Stloc_S, e); gen.Emit(OpCodes.Ldstr, "The field value can\'t be serialized."); gen.Emit(OpCodes.Ldloc_S, e); gen.Emit(OpCodes.Newobj, hproseExceptionCtor); gen.Emit(OpCodes.Throw); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.EndExceptionBlock(); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldloc_S, value); gen.Emit(OpCodes.Call, serializeMethod); } gen.Emit(OpCodes.Ret); serializeFieldsDelegate = (SerializeDelegate)dynamicMethod.CreateDelegate(typeofSerializeDelegate); }
protected override void InitUnserializeDelegate(Type type, string[] names, DynamicMethod dynamicMethod) { Dictionary <string, FieldTypeInfo> fields = HproseHelper.GetFields(type); ILGenerator gen = dynamicMethod.GetILGenerator(); LocalBuilder e = gen.DeclareLocal(typeofException); int count = names.Length; for (int i = 0; i < count; ++i) { FieldTypeInfo field; if (fields.TryGetValue(names[i], out field)) { Label exTryCatch = gen.BeginExceptionBlock(); if (type.IsValueType) { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Unbox, type); } else { gen.Emit(OpCodes.Ldarg_0); } gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4, i); gen.Emit(OpCodes.Ldelem_Ref); if (field.type.IsValueType) { gen.Emit(OpCodes.Unbox_Any, field.type); } gen.Emit(OpCodes.Stfld, field.info); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.BeginCatchBlock(typeofException); gen.Emit(OpCodes.Stloc_S, e); gen.Emit(OpCodes.Ldstr, "The field value can\'t be unserialized."); gen.Emit(OpCodes.Ldloc_S, e); gen.Emit(OpCodes.Newobj, hproseExceptionCtor); gen.Emit(OpCodes.Throw); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.EndExceptionBlock(); } } gen.Emit(OpCodes.Ret); }
private void InitSerializeMembersDelegate(Type type) { ICollection <MemberTypeInfo> members = HproseHelper.GetMembers(type).Values; DynamicMethod dynamicMethod = new DynamicMethod("$SerializeFields", typeofVoid, typeofArgs, type, true); ILGenerator gen = dynamicMethod.GetILGenerator(); LocalBuilder value = gen.DeclareLocal(typeofObject); LocalBuilder e = gen.DeclareLocal(typeofException); foreach (MemberTypeInfo member in members) { Label exTryCatch = gen.BeginExceptionBlock(); if (type.IsValueType) { gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Unbox, type); } else { gen.Emit(OpCodes.Ldarg_0); } if (member.info is FieldInfo) { FieldInfo fieldInfo = (FieldInfo)member.info; gen.Emit(OpCodes.Ldfld, fieldInfo); if (member.type.IsValueType) { gen.Emit(OpCodes.Box, member.type); } } else { PropertyInfo propertyInfo = (PropertyInfo)member.info; MethodInfo getMethod = propertyInfo.GetGetMethod(true); if (getMethod.IsVirtual) { gen.Emit(OpCodes.Callvirt, getMethod); } else { gen.Emit(OpCodes.Call, getMethod); } if (member.type.IsValueType) { gen.Emit(OpCodes.Box, member.type); } } gen.Emit(OpCodes.Stloc_S, value); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.BeginCatchBlock(typeofException); gen.Emit(OpCodes.Stloc_S, e); gen.Emit(OpCodes.Ldstr, "The member value can\'t be serialized."); gen.Emit(OpCodes.Ldloc_S, e); gen.Emit(OpCodes.Newobj, hproseExceptionCtor); gen.Emit(OpCodes.Throw); gen.Emit(OpCodes.Leave_S, exTryCatch); gen.EndExceptionBlock(); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldloc_S, value); gen.Emit(OpCodes.Call, serializeMethod); } gen.Emit(OpCodes.Ret); serializeMembersDelegate = (SerializeDelegate)dynamicMethod.CreateDelegate(typeofSerializeDelegate); }