示例#1
0
        /// <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);
            }
        }
示例#2
0
 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);
         }
     });
 }
示例#3
0
 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);
         }
     });
 }
示例#4
0
 private static DeserializeDelegate deserializeCustom(FieldSerializeHelper field, DeserializerDelegate deserializer)
 {
     return((obj, value) =>
     {
         try
         {
             field.SetValue(obj, deserializer(value));
         }
         catch (FormatException)
         {
             field.SetValue(obj, field.defaultValue);
         }
     });
 }
示例#5
0
        private static SerializeDelegate serializeDefault(FieldSerializeHelper field, string format)
        {
            var formatStr = $"{{0:{format}}}";

            return(obj => string.Format(FormatHelper.DefaultFormat, formatStr, field.GetValue(obj) ?? field.defaultValue));
        }
示例#6
0
 private static SerializeDelegate serializeCustom(FieldSerializeHelper field, SerializeDelegate serializer)
 {
     return(obj => serializer(field.GetValue(obj) ?? field.defaultValue));
 }
示例#7
0
 private static DeserializeDelegate deserializeEnumExact(FieldSerializeHelper field, Type fieldType)
 {
     return((obj, value) => field.SetValue(obj, Enum.Parse(fieldType, value, true)));
 }
示例#8
0
 private static DeserializeDelegate deserializeDefaultExact(FieldSerializeHelper field, Type fieldType)
 {
     return((obj, value) => field.SetValue(obj, Convert.ChangeType(value, fieldType, FormatHelper.DefaultFormat)));
 }
示例#9
0
 private static DeserializeDelegate deserializeCustomExact(FieldSerializeHelper field, DeserializerDelegate deserializer)
 {
     return((obj, value) => field.SetValue(obj, deserializer(value)));
 }