コード例 #1
0
ファイル: xuexueJson.cs プロジェクト: daixian/xuexue.utility
 /// <summary>
 /// 给一个类型绑定一个Json设置
 /// </summary>
 /// <param name="type"></param>
 /// <param name="xxjc"></param>
 public static void BindType(Type type, xuexueJsonClass xxjc)
 {
     if (!dictTypeXXJC.ContainsKey(type))
     {
         dictTypeXXJC.Add(type, xxjc);
     }
     else
     {
         dictTypeXXJC[type] = xxjc;
     }
 }
コード例 #2
0
        private static void AddTypeProperties(Type type)
        {
            if (type_properties.ContainsKey(type))
            {
                return;
            }

            xuexueJsonClass xxjc = type.GetCustomAttribute <xuexueJsonClass>();

            if (xxjc == null)
            {
                //尝试查找用户的定义
                if (JsonTypeRegister.dictTypeXXJC.ContainsKey(type))
                {
                    xxjc = JsonTypeRegister.dictTypeXXJC[type];
                }
            }
            if (xxjc == null)
            {
                xxjc = JsonTypeRegister.defaultClassAttribute;//使用一个当前的默认设置
            }

            List <PropertyMetadata> props = new List <PropertyMetadata>();


            foreach (PropertyInfo p_info in type.GetProperties(xxjc.propertyflags))
            {
                if (p_info.Name == "Item")
                {
                    continue;
                }

                //如果直接在忽略类型列表里了就直接忽略
                if (JsonTypeRegister.listIgnoreClass.Contains(p_info.PropertyType) ||
                    JsonTypeRegister.listIgnoreClass.Contains(p_info.PropertyType.GetElementType()))
                {
                    continue;
                }

                //对System.Collections.Generic中的数据结构进行忽略判断
                if (p_info.PropertyType.IsGenericType)
                {
                    bool   isIgnore = false;
                    Type[] gat      = p_info.PropertyType.GetGenericArguments();
                    for (int i = 0; i < gat.Length; i++)
                    {
                        if (JsonTypeRegister.listIgnoreClass.Contains(gat[i]))
                        {
                            isIgnore = true;
                            break;
                        }
                    }
                    if (isIgnore)
                    {
                        continue;
                    }
                }

                //如果存在特别要使能的成员名List
                if (xxjc.enableMembers != null && xxjc.enableMembers.Contains(p_info.Name))
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info     = p_info;
                    p_data.IsField  = false;
                    p_data.Priority = 0;
                    props.Add(p_data);

                    continue;
                }

                if (xxjc.ignoreMembers != null && xxjc.ignoreMembers.Contains(p_info.Name))
                {
                    continue; //应该被忽略
                }

                if (p_info.IsDefined(typeof(xuexueJson), false))
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info     = p_info;
                    p_data.IsField  = false;
                    p_data.Priority = p_info.GetCustomAttribute <xuexueJson>().priority;
                    props.Add(p_data);
                    continue;
                }
                if (p_info.IsDefined(typeof(xuexueJsonIgnore), false))
                {
                    continue;
                }
                if (xxjc.defaultPropertyConstraint == true)//如果默认设置是开启
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info     = p_info;
                    p_data.IsField  = false;
                    p_data.Priority = 0;
                    props.Add(p_data);
                }
            }

            foreach (FieldInfo f_info in type.GetFields(xxjc.fieldflags))
            {
                //如果直接在忽略类型列表里了就直接忽略
                if (JsonTypeRegister.listIgnoreClass.Contains(f_info.FieldType) ||
                    JsonTypeRegister.listIgnoreClass.Contains(f_info.FieldType.GetElementType()))
                {
                    continue;
                }

                //对System.Collections.Generic中的数据结构进行忽略判断
                if (f_info.FieldType.IsGenericType)
                {
                    bool   isIgnore = false;
                    Type[] gat      = f_info.FieldType.GetGenericArguments();
                    for (int i = 0; i < gat.Length; i++)
                    {
                        if (JsonTypeRegister.listIgnoreClass.Contains(gat[i]))
                        {
                            isIgnore = true;
                            break;
                        }
                    }
                    if (isIgnore)
                    {
                        continue;
                    }
                }

                if (xxjc.enableMembers != null && xxjc.enableMembers.Contains(f_info.Name))
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info     = f_info;
                    p_data.IsField  = true;
                    p_data.Priority = 0;
                    props.Add(p_data);

                    continue;
                }
                if (xxjc.ignoreMembers != null && xxjc.ignoreMembers.Contains(f_info.Name))
                {
                    continue; //应该被忽略
                }

                if (f_info.IsDefined(typeof(xuexueJson), false))
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info     = f_info;
                    p_data.IsField  = true;
                    p_data.Priority = f_info.GetCustomAttribute <xuexueJson>().priority;
                    props.Add(p_data);
                    continue; //应该被忽略
                }
                if (f_info.IsDefined(typeof(xuexueJsonIgnore), false))
                {
                    continue;
                }
                if (xxjc.defaultFieldConstraint == true)//如果默认设置是开启
                {
                    PropertyMetadata p_data = new PropertyMetadata();
                    p_data.Info     = f_info;
                    p_data.IsField  = true;
                    p_data.Priority = 0;
                    props.Add(p_data);
                }
            }

            props.Sort((p1, p2) => { return(p1.Priority - p2.Priority); });

            lock (type_properties_lock) {
                try {
                    type_properties.Add(type, props);
                } catch (ArgumentException) {
                    return;
                }
            }
        }
コード例 #3
0
        private static void AddObjectMetadata(Type type)
        {
            if (object_metadata.ContainsKey(type))
            {
                return;
            }

            xuexueJsonClass xxjc = type.GetCustomAttribute <xuexueJsonClass>();

            if (xxjc == null)
            {
                //尝试查找用户的定义
                if (JsonTypeRegister.dictTypeXXJC.ContainsKey(type))
                {
                    xxjc = JsonTypeRegister.dictTypeXXJC[type];
                }
            }
            if (xxjc == null)
            {
                xxjc = JsonTypeRegister.defaultClassAttribute;//使用一个当前的默认设置
            }

            IList <PropertyMetadata> props = new List <PropertyMetadata>();

            ObjectMetadata data = new ObjectMetadata();

            if (type.GetInterface("System.Collections.IDictionary") != null)
            {
                data.IsDictionary = true;
            }

            data.Properties = new Dictionary <string, PropertyMetadata> ();

            foreach (PropertyInfo p_info in type.GetProperties(xxjc.propertyflags))
            {
                if (p_info.Name == "Item")
                {
                    ParameterInfo[] parameters = p_info.GetIndexParameters();

                    if (parameters.Length != 1)
                    {
                        continue;
                    }

                    if (parameters[0].ParameterType == typeof(string))
                    {
                        data.ElementType = p_info.PropertyType;
                    }

                    continue;
                }

                PropertyMetadata p_data = new PropertyMetadata();
                p_data.Info = p_info;
                p_data.Type = p_info.PropertyType;

                data.Properties.Add(p_info.Name, p_data);
            }

            foreach (FieldInfo f_info in type.GetFields(xxjc.fieldflags))
            {
                PropertyMetadata p_data = new PropertyMetadata();
                p_data.Info    = f_info;
                p_data.IsField = true;
                p_data.Type    = f_info.FieldType;

                data.Properties.Add(f_info.Name, p_data);
            }

            lock (object_metadata_lock) {
                try {
                    object_metadata.Add(type, data);
                } catch (ArgumentException) {
                    return;
                }
            }
        }