Exemple #1
0
 static object GetInstanceValue(object obj, Type t, Getters p)
 {
     if (t.IsValueType && p.FieldInfo != null)
     {
         return(p.FieldInfo.GetValue(obj));
     }
     if (t.IsValueType && p.PropertyType != null)
     {
         return(t.GetProperty(p.Name, BindingFlags.Public | BindingFlags.Instance)?.GetValue(obj, null));
     }
     return(p.Getter(obj));
 }
Exemple #2
0
        /// <summary>
        /// Return a list of propery/field access proxies for a type
        /// </summary>
        internal List <Getters> GetGetters(Type type)
        {
            List <Getters> val;

            if (getterCache.TryGetValue(type, out val))
            {
                return(val);
            }

            var props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            var getters = (from p in props where p.CanWrite || jsonParameters.ShowReadOnlyProperties || jsonParameters.EnableAnonymousTypes
                           let att = p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false)
                                     where att.Length <= 0 let g = CreateGetMethod(p) where g != null

                                                                   select new Getters {
                Name = p.Name, Getter = g, PropertyType = p.PropertyType
            }).ToList();

            var fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public);

            foreach (var f in fi)
            {
                var att = f.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att.Length > 0)
                {
                    continue;
                }

                var g = CreateGetField(type, f);
                if (g == null)
                {
                    continue;
                }
                var gg = new Getters {
                    Name = f.Name, Getter = g, PropertyType = f.FieldType, FieldInfo = f
                };
                getters.Add(gg);
            }

            getterCache.Add(type, getters);
            return(getters);
        }