示例#1
0
        internal List <Getters> GetGetters(Type type)
        {
            List <Getters> val = null;

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

            PropertyInfo[] props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <Getters> getters = new List <Getters>();

            foreach (PropertyInfo p in props)
            {
                if (!p.CanWrite)
                {
                    continue;
                }

                JSON.GenericGetter g = CreateGetMethod(p);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name         = p.Name;
                    gg.Getter       = g;
                    gg.propertyType = p.PropertyType;
                    getters.Add(gg);
                }
            }
            _getterscache.Add(type, getters);
            return(getters);
        }
示例#2
0
        internal Getters[] GetGetters(Type type, bool showreadonly)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
            {
                return(val);
            }

            PropertyInfo[] props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            List <Getters> getters = new List <Getters>();

            foreach (PropertyInfo p in props)
            {
                if (!p.CanWrite && showreadonly == false)
                {
                    continue;
                }

                                #if !SILVERLIGHT && USE_XML
                object[] att = p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att != null && att.Length > 0)
                {
                    continue;
                }
                                #endif
                GenericGetter g = CreateGetMethod(type, p);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name   = p.Name;
                    gg.Getter = g;
                    //gg.propertyType = p.PropertyType;
                    getters.Add(gg);
                }
            }

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
            foreach (var f in fi)
            {
                                #if !SILVERLIGHT && USE_XML
                object[] att = f.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att != null && att.Length > 0)
                {
                    continue;
                }
                                #endif
                GenericGetter g = CreateGetField(type, f);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name   = f.Name;
                    gg.Getter = g;
                    //gg.propertyType = f.FieldType;
                    getters.Add(gg);
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return(val);
        }
示例#3
0
 public List <Getters> GetGetters(Type type)
 {
     if (_getterscache.ContainsKey(type))
     {
         return(_getterscache[type]);
     }
     else
     {
         PropertyInfo[] props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
         List <Getters> getters = new List <Getters>();
         foreach (PropertyInfo p in props)
         {
             GenericGetter g = CreateGetMethod(p);
             if (g != null)
             {
                 Getters gg = new Getters();
                 gg.Name   = p.Name;
                 gg.Getter = g;
                 getters.Add(gg);
             }
         }
         _getterscache.Add(type, getters);
         return(getters);
     }
 }
示例#4
0
        internal List <Getters> GetGetters(Type type)
        {
            List <Getters> val = null;

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

            PropertyInfo[] props   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            List <Getters> getters = new List <Getters>();

            foreach (PropertyInfo p in props)
            {
                if (!p.CanWrite && _params.ShowReadOnlyProperties == false && _params.EnableAnonymousTypes == false)
                {
                    continue;
                }

                object[] att = p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att != null && att.Length > 0)
                {
                    continue;
                }

                JSON.GenericGetter g = CreateGetMethod(p);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name         = p.Name;
                    gg.Getter       = g;
                    gg.propertyType = p.PropertyType;
                    getters.Add(gg);
                }
            }

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (var f in fi)
            {
                object[] att = f.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att != null && att.Length > 0)
                {
                    continue;
                }

                JSON.GenericGetter g = CreateGetField(type, f);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name         = f.Name;
                    gg.Getter       = g;
                    gg.propertyType = f.FieldType;
                    getters.Add(gg);
                }
            }

            _getterscache.Add(type, getters);
            return(getters);
        }
示例#5
0
        public Getters[] GetGetters(Type type, bool showreadonly)
        {
            Getters[] val = null;
            if (_getterscache.TryGetValue(type, out val))
                return val;

            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
            List<Getters> getters = new List<Getters>();
            foreach (PropertyInfo p in props)
            {
                if (!p.CanWrite && showreadonly == false) continue;

                object[] att = p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att != null && att.Length > 0)
                    continue;

                GenericGetter g = CreateGetMethod(type, p);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name = p.Name;
                    gg.Getter = g;
                    //gg.propertyType = p.PropertyType;
                    getters.Add(gg);
                }
            }

            FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
            foreach (var f in fi)
            {
                object[] att = f.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false);
                if (att != null && att.Length > 0)
                    continue;

                GenericGetter g = CreateGetField(type, f);
                if (g != null)
                {
                    Getters gg = new Getters();
                    gg.Name = f.Name;
                    gg.Getter = g;
                    //gg.propertyType = f.FieldType;
                    getters.Add(gg);
                }
            }
            val = getters.ToArray();
            _getterscache.Add(type, val);
            return val;
        }
示例#6
0
        internal List<Getters> GetGetters(Type type)
        {
            if(_getterscache.ContainsKey(type))
                return _getterscache[type];
            else
            {
                PropertyInfo[] props = type.GetProperties(BindingFlags.Public| BindingFlags.Instance);
                List<Getters> getters = new List<Getters>();
                foreach(PropertyInfo p in props)
                {
                    GenericGetter g = CreateGetMethod(p);
                    if(g!=null)
                    {
                        Getters gg = new Getters();
                        gg.Name = p.Name;
                        gg.Getter = g;
                        getters.Add(gg);
                    }

                }
                _getterscache.Add(type,getters);
                return getters;
            }
        }
示例#7
0
        internal List<Getters> GetGetters(Type type)
        {
            List<Getters> getters = null;
            if (_getterscache.TryGetValue(type,out getters))
                return getters;
            else
            {
                PropertyInfo[] props = type.GetProperties(BindingFlags.Public| BindingFlags.Instance);
                getters = new List<Getters>(props.Length);
                foreach(PropertyInfo p in props)
                {
                    GenericGetter g = CreateGetMethod(p);
                    if(g!=null)
                    {
                        Getters gg = new Getters();
                        gg.Name = p.Name;
                        gg.Getter = g;
                        getters.Add(gg);
                    }

                }
                _getterscache.Add(type,getters);
                return getters;
            }
        }
示例#8
0
        private void WriteObject(object obj)
        {
            int i = 0;

            if (_cirobj.TryGetValue(obj, out i) == false)
            {
                _cirobj.Add(obj, _cirobj.Count + 1);
            }
            else
            {
                if (_current_depth > 0 && _params.InlineCircularReferences == false)
                {
                    //_circular = true;
                    _output.Append("{\"$i\":");
                    _output.Append(i.ToString());
                    _output.Append("}");
                    return;
                }
            }
            if (_params.UsingGlobalTypes == false)
            {
                _output.Append('{');
            }
            else
            {
                if (_TypesWritten == false)
                {
                    _output.Append('{');
                    _before = _output.Length;
                    //_output = new StringBuilder();
                }
                else
                {
                    _output.Append('{');
                }
            }
            _TypesWritten = true;
            _current_depth++;
            if (_current_depth > _MAX_DEPTH)
            {
                throw new Exception("Serializer encountered maximum depth of " + _MAX_DEPTH);
            }


            Dictionary <string, string> map = new Dictionary <string, string>();
            Type t      = obj.GetType();
            bool append = false;

            if (_params.UseExtensions)
            {
                if (_params.UsingGlobalTypes == false)
                {
                    WritePairFast("$type", Reflection.Instance.GetTypeAssemblyName(t));
                }
                else
                {
                    int    dt = 0;
                    string ct = Reflection.Instance.GetTypeAssemblyName(t);
                    if (_globalTypes.TryGetValue(ct, out dt) == false)
                    {
                        dt = _globalTypes.Count + 1;
                        _globalTypes.Add(ct, dt);
                    }
                    WritePairFast("$type", dt.ToString());
                }
                append = true;
            }

            Getters[] g = Reflection.Instance.GetGetters(t, _params.ShowReadOnlyProperties, _params.IgnoreAttributes);
            int       c = g.Length;

            for (int ii = 0; ii < c; ii++)
            {
                Getters p = g[ii];
                object  o = p.getValue(obj);

                if (_params.SerializeNullValues == false && (o == null || o is DBNull))
                {
                    //append = false;
                }
                else
                {
                    if (append)
                    {
                        _output.Append(',');
                    }
                    if (_params.SerializeToLowerCaseNames)
                    {
                        WritePair(p.lcName, o);
                    }
                    else
                    {
                        WritePair(p.Name, o);
                    }
                    if (o != null && _params.UseExtensions)
                    {
                        Type tt = o.GetType();
                        if (tt == typeof(System.Object))
                        {
                            map.Add(p.Name, tt.ToString());
                        }
                    }
                    append = true;
                }
            }
            if (map.Count > 0 && _params.UseExtensions)
            {
                _output.Append(",\"$map\":");
                WriteStringDictionary(map);
            }
            _output.Append('}');
            _current_depth--;
        }