/// <summary> /// 获取属性成员集合 /// </summary> /// <param name="properties"></param> /// <param name="typeAttribute">类型配置</param> /// <returns>属性成员集合</returns> public static LeftArray <PropertyMethod> GetProperties(PropertyIndex[] properties, SerializeAttribute typeAttribute) { LeftArray <PropertyMethod> values = new LeftArray <PropertyMethod>(properties.Length); foreach (PropertyIndex property in properties) { if (property.Member.CanWrite) { Type type = property.Member.PropertyType; if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !property.IsIgnore && !typeof(Delegate).IsAssignableFrom(type) && !property.Member.IsDefined(typeof(IgnoreMemberAttribute), typeAttribute.IsBaseTypeAttribute)) { MemberAttribute attribute = property.GetAttribute <MemberAttribute>(typeAttribute.IsBaseTypeAttribute); if (typeAttribute.IsAttribute ? (attribute != null && attribute.IsSetup) : (attribute == null || attribute.IsSetup)) { MethodInfo method = property.Member.GetSetMethod(true); if (method != null && method.GetParameters().Length == 1) { values.Add(new PropertyMethod { Property = property, Method = method, Attribute = attribute }); } } } } } return(values); }
/// <summary> /// 添加成员 /// </summary> /// <param name="name">成员名称</param> /// <param name="attribute">XML序列化成员配置</param> private void nameStart(string name, MemberAttribute attribute) { WriteName(generator, OpCodes.Ldloc_0, name, false); if (attribute != null && attribute.ItemName != null) { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldstr, attribute.ItemName); generator.Emit(OpCodes.Stfld, ItemNameField); } }
/// <summary> /// 添加成员 /// </summary> /// <param name="name">成员名称</param> /// <param name="attribute">XML序列化成员配置</param> private void nameStart(string name, MemberAttribute attribute) { generator.charStreamSimpleWriteNotNull(OpCodes.Ldloc_0, SerializeMethodCache.GetNameStartPool(name), name.Length + 2); if (attribute != null && attribute.ItemName != null) { generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Ldstr, attribute.ItemName); generator.Emit(OpCodes.Stfld, ItemNameField); } }
/// <summary> /// 获取字段成员集合 /// </summary> /// <param name="fields"></param> /// <param name="typeAttribute">类型配置</param> /// <returns>字段成员集合</returns> public static LeftArray <KeyValue <FieldIndex, MemberAttribute> > GetFields(FieldIndex[] fields, SerializeAttribute typeAttribute) { LeftArray <KeyValue <FieldIndex, MemberAttribute> > values = new LeftArray <KeyValue <FieldIndex, MemberAttribute> >(fields.Length); foreach (FieldIndex field in fields) { Type type = field.Member.FieldType; if (!type.IsPointer && (!type.IsArray || type.GetArrayRank() == 1) && !field.IsIgnore && !typeof(Delegate).IsAssignableFrom(type) && !field.Member.IsDefined(typeof(IgnoreMemberAttribute), typeAttribute.IsBaseTypeAttribute)) { MemberAttribute attribute = field.GetAttribute <MemberAttribute>(typeAttribute.IsBaseTypeAttribute); if (typeAttribute.IsAttribute ? (attribute != null && attribute.IsSetup) : (attribute == null || attribute.IsSetup)) { values.Add(new KeyValue <FieldIndex, MemberAttribute>(field, attribute)); } } } return(values); }
/// <summary> /// 添加字段 /// </summary> /// <param name="field">字段信息</param> /// <param name="attribute">XML序列化成员配置</param> public void Push(FieldIndex field, MemberAttribute attribute) { Label end = default(Label); MethodInfo isOutputMethod = SerializeMethodCache.GetIsOutputMethod(field.Member.FieldType); if (isOutputMethod != null) { generator.Emit(OpCodes.Ldarg_0); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 1); } else { generator.Emit(OpCodes.Ldarg_1); } generator.Emit(OpCodes.Ldfld, field.Member); generator.call(isOutputMethod); generator.Emit(OpCodes.Brfalse, end = generator.DefineLabel()); } string name = field.AnonymousName; nameStart(name, attribute); bool isCustom = false; MethodInfo method = SerializeMethodCache.GetMemberMethodInfo(field.Member.FieldType, ref isCustom); if (isCustom) { if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 1); } else { generator.Emit(OpCodes.Ldarg_1); } generator.Emit(OpCodes.Ldflda, field.Member); generator.Emit(OpCodes.Ldarg_0); } else { generator.Emit(OpCodes.Ldarg_0); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 1); } else { generator.Emit(OpCodes.Ldarg_1); } generator.Emit(OpCodes.Ldfld, field.Member); } generator.call(method); WriteName(generator, OpCodes.Ldloc_0, name, true); if (isOutputMethod != null) { generator.MarkLabel(end); } }
/// <summary> /// 添加属性 /// </summary> /// <param name="property">属性信息</param> /// <param name="propertyMethod">函数信息</param> /// <param name="attribute">XML序列化成员配置</param> public void Push(PropertyIndex property, MethodInfo propertyMethod, MemberAttribute attribute) { Label end = default(Label); MethodInfo isOutputMethod = SerializeMethodCache.GetIsOutputMethod(property.Member.PropertyType); if (isOutputMethod != null) { generator.Emit(OpCodes.Ldarg_0); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 1); } else { generator.Emit(OpCodes.Ldarg_1); } generator.call(propertyMethod); generator.call(isOutputMethod); generator.Emit(OpCodes.Brfalse, end = generator.DefineLabel()); } nameStart(property.Member.Name, attribute); bool isCustom = false; MethodInfo method = SerializeMethodCache.GetMemberMethodInfo(property.Member.PropertyType, ref isCustom); if (isCustom) { LocalBuilder loadMember = generator.DeclareLocal(property.Member.PropertyType); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 1); } else { generator.Emit(OpCodes.Ldarg_1); } generator.call(propertyMethod); generator.Emit(OpCodes.Stloc_0); generator.Emit(OpCodes.Ldloca_S, loadMember); generator.Emit(OpCodes.Ldarg_0); } else { generator.Emit(OpCodes.Ldarg_0); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 1); } else { generator.Emit(OpCodes.Ldarg_1); } generator.call(propertyMethod); } generator.call(method); WriteName(generator, OpCodes.Ldloc_0, property.Member.Name, true); if (isOutputMethod != null) { generator.MarkLabel(end); } }
/// <summary> /// 添加字段 /// </summary> /// <param name="field">字段信息</param> /// <param name="attribute">XML序列化成员配置</param> public void Push(FieldIndex field, MemberAttribute attribute) { Label end = generator.DefineLabel(); generator.memberMapIsMember(OpCodes.Ldarg_0, field.MemberIndex); generator.Emit(OpCodes.Brfalse, end); MethodInfo isOutputMethod = SerializeMethodCache.GetIsOutputMethod(field.Member.FieldType); if (isOutputMethod != null) { generator.Emit(OpCodes.Ldarg_1); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 2); } else { generator.Emit(OpCodes.Ldarg_2); } generator.Emit(OpCodes.Ldfld, field.Member); generator.call(isOutputMethod); generator.Emit(OpCodes.Brfalse, end); } string name = field.AnonymousName; SerializeMemberDynamicMethod.WriteName(generator, OpCodes.Ldloc_0, name, false); if (attribute != null && attribute.ItemName != null) { generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Ldstr, attribute.ItemName); generator.Emit(OpCodes.Stfld, SerializeMemberDynamicMethod.ItemNameField); } bool isCustom = false; MethodInfo method = SerializeMethodCache.GetMemberMethodInfo(field.Member.FieldType, ref isCustom); if (isCustom) { if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 2); } else { generator.Emit(OpCodes.Ldarg_2); } generator.Emit(OpCodes.Ldflda, field.Member); generator.Emit(OpCodes.Ldarg_1); } else { generator.Emit(OpCodes.Ldarg_1); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 2); } else { generator.Emit(OpCodes.Ldarg_2); } generator.Emit(OpCodes.Ldfld, field.Member); } generator.call(method); SerializeMemberDynamicMethod.WriteName(generator, OpCodes.Ldloc_0, name, true); generator.MarkLabel(end); }
/// <summary> /// 添加属性 /// </summary> /// <param name="property">属性信息</param> /// <param name="propertyMethod">函数信息</param> /// <param name="attribute">XML序列化成员配置</param> public void Push(PropertyIndex property, MethodInfo propertyMethod, MemberAttribute attribute) { Label end = generator.DefineLabel(); generator.memberMapIsMember(OpCodes.Ldarg_0, property.MemberIndex); generator.Emit(OpCodes.Brfalse, end); MethodInfo isOutputMethod = SerializeMethodCache.GetIsOutputMethod(property.Member.PropertyType); if (isOutputMethod != null) { generator.Emit(OpCodes.Ldarg_1); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 2); } else { generator.Emit(OpCodes.Ldarg_2); } generator.call(propertyMethod); generator.call(isOutputMethod); generator.Emit(OpCodes.Brfalse, end); } SerializeMemberDynamicMethod.WriteName(generator, OpCodes.Ldloc_0, property.Member.Name, false); if (attribute != null && attribute.ItemName != null) { generator.Emit(OpCodes.Ldarg_1); generator.Emit(OpCodes.Ldstr, attribute.ItemName); generator.Emit(OpCodes.Stfld, SerializeMemberDynamicMethod.ItemNameField); } bool isCustom = false; MethodInfo method = SerializeMethodCache.GetMemberMethodInfo(property.Member.PropertyType, ref isCustom); if (isCustom) { LocalBuilder loadMember = generator.DeclareLocal(property.Member.PropertyType); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 2); } else { generator.Emit(OpCodes.Ldarg_2); } generator.call(propertyMethod); generator.Emit(OpCodes.Stloc_0); generator.Emit(OpCodes.Ldloca_S, loadMember); generator.Emit(OpCodes.Ldarg_1); } else { generator.Emit(OpCodes.Ldarg_1); if (isValueType) { generator.Emit(OpCodes.Ldarga_S, 2); } else { generator.Emit(OpCodes.Ldarg_2); } generator.call(propertyMethod); } generator.call(method); SerializeMemberDynamicMethod.WriteName(generator, OpCodes.Ldloc_0, property.Member.Name, true); generator.MarkLabel(end); }