/// <summary> /// Create new instance of <see cref="Entry"/>. /// </summary> protected Entry() { var type = this.GetType(); if (!fieldInfoCache.TryGetValue(type, out this.fieldInfo)) { this.fieldInfo = new Dictionary <string, FieldSerializeHelper>(StringComparer.OrdinalIgnoreCase); do { foreach (var fInfo in type.GetRuntimeFields()) { var att = fInfo.GetCustomAttribute <EntryFieldAttribute>(); if (att == null) { continue; } var ser = fInfo.GetCustomAttribute <SerializeAttribute>(); var helper = new FieldSerializeHelper(fInfo, att, ser); this.fieldInfo.Add(att.Name, helper); if (!string.IsNullOrEmpty(att.Alias)) { this.fieldInfo.Add(att.Alias, helper); } } } while((type = type.GetTypeInfo().BaseType) != typeof(Entry)); fieldInfoCache.Add(this.GetType(), this.fieldInfo); } }
private static DeserializeDelegate deserializeDefault(FieldSerializeHelper field, Type fieldType) { return((obj, value) => { try { field.SetValue(obj, Convert.ChangeType(value, fieldType, FormatHelper.DefaultFormat)); } catch (FormatException) { field.SetValue(obj, field.defaultValue); } }); }
private static DeserializeDelegate deserializeEnum(FieldSerializeHelper field, Type fieldType) { return((obj, value) => { try { field.SetValue(obj, Enum.Parse(fieldType, value, true)); } catch (FormatException) { field.SetValue(obj, field.defaultValue); } }); }
private static DeserializeDelegate deserializeCustom(FieldSerializeHelper field, DeserializerDelegate deserializer) { return((obj, value) => { try { field.SetValue(obj, deserializer(value)); } catch (FormatException) { field.SetValue(obj, field.defaultValue); } }); }
private static SerializeDelegate serializeDefault(FieldSerializeHelper field, string format) { var formatStr = $"{{0:{format}}}"; return(obj => string.Format(FormatHelper.DefaultFormat, formatStr, field.GetValue(obj) ?? field.defaultValue)); }
private static SerializeDelegate serializeCustom(FieldSerializeHelper field, SerializeDelegate serializer) { return(obj => serializer(field.GetValue(obj) ?? field.defaultValue)); }
private static DeserializeDelegate deserializeEnumExact(FieldSerializeHelper field, Type fieldType) { return((obj, value) => field.SetValue(obj, Enum.Parse(fieldType, value, true))); }
private static DeserializeDelegate deserializeDefaultExact(FieldSerializeHelper field, Type fieldType) { return((obj, value) => field.SetValue(obj, Convert.ChangeType(value, fieldType, FormatHelper.DefaultFormat))); }
private static DeserializeDelegate deserializeCustomExact(FieldSerializeHelper field, DeserializerDelegate deserializer) { return((obj, value) => field.SetValue(obj, deserializer(value))); }