Ejemplo n.º 1
0
 /// <summary>
 /// 获取指定类型的所有属性元数据。
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public virtual List <SerializerPropertyMetadata> GetProperties(Type type)
 {
     return(_cache.GetOrAdd(type, k =>
     {
         return k.GetProperties()
         .Where(s => s.CanRead && !SerializerUtil.IsNoSerializable(_option, s))
         .Distinct(new SerializerUtil.PropertyEqualityComparer())
         .Select(s => new SerializerPropertyMetadata
         {
             Filter = (p, l) =>
             {
                 return !SerializerUtil.CheckLazyValueCreate(l, p.Name);
             },
             Getter = o => ReflectionCache.GetAccessor(s).GetValue(o),
             Setter = (o, v) => ReflectionCache.GetAccessor(s).SetValue(o, v),
             PropertyInfo = s,
             PropertyName = ResolvePropertyName(s),
             Formatter = s.GetCustomAttributes <TextFormatterAttribute>().FirstOrDefault()?.Formatter,
             DefaultValue = s.GetCustomAttributes <DefaultValueAttribute>().FirstOrDefault()?.Value,
             Converter = s.GetCustomAttributes <TextPropertyConverterAttribute>().FirstOrDefault()?.ConverterType.New <ITextConverter>()
         })
         .Where(s => !string.IsNullOrEmpty(s.PropertyName))
         .ToList();
     }));
 }
Ejemplo n.º 2
0
        public string Parse(object schema, object profile, string output)
        {
            var mappers = cache.TryGetValue(output, () =>
            {
                var list        = new List <AccessorMap>();
                var schemaType  = schema != null ? schema.GetType() : null;
                var profileType = profile != null ? profile.GetType() : null;
                var regex       = new Regex(@"\{(\w+)\}");
                var matches     = regex.Matches(output);
                foreach (Match match in matches)
                {
                    var group = match.Groups[0];
                    var name  = group.Value.Substring(1, group.Value.Length - 2);

                    if (schemaType != null)
                    {
                        var property = schemaType.GetProperty(name);
                        if (property != null)
                        {
                            list.Add(new AccessorMap
                            {
                                GroupName  = group.Value,
                                Accessor   = ReflectionCache.GetAccessor(property),
                                ObjectType = ObjectType.Schema
                            });
                        }
                    }

                    if (profileType != null)
                    {
                        var property = profileType.GetProperty(name);
                        if (property != null)
                        {
                            list.Add(new AccessorMap
                            {
                                GroupName  = group.Value,
                                Accessor   = ReflectionCache.GetAccessor(property),
                                ObjectType = ObjectType.Profile
                            });
                        }
                    }
                }

                return(list);
            });

            foreach (var p in mappers)
            {
                var value = GetPropertyValue(schema, profile, p);
                output = output.Replace(p.GroupName, value);
            }

            return(output);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取指定类型的属性访问缓存。
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private Dictionary <string, PropertyAccessor> GetAccessorCache(Type type)
        {
            return(context.SetAccessors.TryGetValue(type, () =>
            {
                return type.GetProperties()
                .Where(s => s.CanWrite && !SerializerUtil.IsNoSerializable(option, s))
                .Select(s =>
                {
                    var ele = s.GetCustomAttributes <TextSerializeElementAttribute>().FirstOrDefault();
                    //如果使用Camel命名,则名称第一位小写
                    var name = ele != null ?
                               ele.Name : (option.CamelNaming ?
                                           char.ToLower(s.Name[0]) + s.Name.Substring(1) : s.Name);

                    return new { name, p = s };
                })
                .ToDictionary(s => s.name, s => ReflectionCache.GetAccessor(s.p));
            }));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 获取指定类型的属性访问缓存。
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 private List <PropertyGetAccessorCache> GetAccessorCache(Type type)
 {
     return(context.GetAccessors.TryGetValue(type, () =>
     {
         return type.GetProperties()
         .Where(s => s.CanRead && !SerializerUtil.IsNoSerializable(option, s))
         .Distinct(new SerializerUtil.PropertyEqualityComparer())
         .Select(s => new PropertyGetAccessorCache
         {
             Accessor = ReflectionCache.GetAccessor(s),
             Filter = (p, l) =>
             {
                 return !SerializerUtil.CheckLazyValueCreate(l, p.Name);
             },
             PropertyInfo = s,
             PropertyName = SerializerUtil.GetPropertyName(s)
         })
         .Where(s => !string.IsNullOrEmpty(s.PropertyName))
         .ToList();
     }));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 获取指定类型的属性访问缓存。
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public List <PropertyGetAccessorCache> GetAccessorCache(Type type)
 {
     return(GetAccessors.TryGetValue(type, () =>
     {
         return type.GetProperties()
         .Where(s => s.CanRead && !SerializerUtil.IsNoSerializable(Option, s))
         .Distinct(new SerializerUtil.PropertyEqualityComparer())
         .Select(s => new PropertyGetAccessorCache
         {
             Accessor = ReflectionCache.GetAccessor(s),
             Filter = (p, l) =>
             {
                 return !SerializerUtil.CheckLazyValueCreate(l, p.Name);
             },
             PropertyInfo = s,
             PropertyName = SerializerUtil.GetPropertyName(s),
             Formatter = s.GetCustomAttributes <TextFormatterAttribute>().FirstOrDefault()?.Formatter,
             DefaultValue = s.GetCustomAttributes <DefaultValueAttribute>().FirstOrDefault()?.Value,
             Converter = s.GetCustomAttributes <TextPropertyConverterAttribute>().FirstOrDefault()?.ConverterType.New <ITextConverter>()
         })
         .Where(s => !string.IsNullOrEmpty(s.PropertyName))
         .ToList();
     }));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 高效地获取指定字段的值。
 /// </summary>
 /// <param name="field">要操作的字段。</param>
 /// <param name="instance">实例对象。</param>
 /// <returns></returns>
 public static object FastGetValue(this FieldInfo field, object instance)
 {
     Guard.ArgumentNull(field, nameof(field));
     return(ReflectionCache.GetAccessor(field).GetValue(instance));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 高效地设置指定属性的值。
 /// </summary>
 /// <param name="property">要操作的属性。</param>
 /// <param name="instance">实例对象。</param>
 /// <param name="value">要设置的值。</param>
 public static void FastSetValue(this PropertyInfo property, object instance, object value)
 {
     Guard.ArgumentNull(property, nameof(property));
     ReflectionCache.GetAccessor(property).SetValue(instance, value);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 高效地获取指定属性的值。
 /// </summary>
 /// <param name="property">要操作的属性。</param>
 /// <param name="instance">实例对象。</param>
 /// <returns></returns>
 public static object FastGetValue(this PropertyInfo property, object instance)
 {
     Guard.ArgumentNull(property, nameof(property));
     return(ReflectionCache.GetAccessor(property).GetValue(instance));
 }