예제 #1
0
        public static AldNode SerializeField(string key, object obj)
        {
            AldNode data = new AldNode(key, string.Empty);

            if (obj == null)
            {
                return(data);
            }
            Type type = obj.GetType();

            if (!type.IsValueType && !type.IsSealed)
            {
                data.Type = TypeToName(type);
            }
            bool expand = type.IsDefined(expandAttributeType, true);

            if (obj is IDictionary)
            {
                IDictionary dict      = obj as IDictionary;
                Hashtable   hashtable = new Hashtable(dict);
                foreach (DictionaryEntry pair in hashtable)
                {
                    data.Add(SerializeField(pair.Key.ToString(), pair.Value));
                }
            }
            else if (obj is IList)
            {
                IList list = obj as IList;
                for (int i = 0; i < list.Count; i++)
                {
                    data.Add(SerializeField(i.ToString(), list[i]));
                }
            }
            else if (obj is DateTime)
            {
                data.Value = obj.ToString();
            }
            else if (((AldSettings.AutoSerializeStructs || expand) && type.IsValueType && !type.IsPrimitive && !type.IsEnum) || (type.IsClass && expand))
            {
                data = Serialize(key, obj);
            }
            else
            {
                data.Value = obj.ToString();
            }
            return(data);
        }
예제 #2
0
        public static AldNode Serialize(string key, object obj)
        {
            AldNode data = new AldNode(key, string.Empty);
            Type    type = obj.GetType();

            if (!type.IsValueType && !type.IsSealed)
            {
                data.Type = TypeToName(type);
            }
            PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (PropertyInfo prop in props)
            {
                if (!prop.IsDefined(serializeAttributeType, true))
                {
                    continue;
                }
                if (prop.IsDefined(ignoreAttributeType, true))
                {
                    continue;
                }
                object value = prop.GetValue(obj, null);
                data.Add(SerializeField(prop.Name, value));
            }
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            foreach (FieldInfo field in fields)
            {
                if (!field.IsPublic && !field.IsDefined(serializeAttributeType, true))
                {
                    continue;
                }
                if (field.IsDefined(ignoreAttributeType, true))
                {
                    continue;
                }
                object value = field.GetValue(obj);
                data.Add(SerializeField(field.Name, value));
            }
            return(data);
        }