예제 #1
0
        public static MDataColumn GetColumns(Type typeInfo)
        {
            string key = "ColumnCache_" + typeInfo.FullName;

            if (_ColumnCache.ContainsKey(key))
            {
                return(_ColumnCache[key].Clone());
            }
            else
            {
                #region 获取列结构
                MDataColumn mdc = new MDataColumn();
                mdc.TableName = typeInfo.Name;
                switch (StaticTool.GetSystemType(ref typeInfo))
                {
                case SysType.Base:
                case SysType.Enum:
                    mdc.Add(typeInfo.Name, DataType.GetSqlType(typeInfo), false);
                    return(mdc);

                case SysType.Generic:
                case SysType.Collection:
                    Type[] argTypes;
                    Tool.StaticTool.GetArgumentLength(ref typeInfo, out argTypes);
                    foreach (Type type in argTypes)
                    {
                        mdc.Add(type.Name, DataType.GetSqlType(type), false);
                    }
                    argTypes = null;
                    return(mdc);
                }

                List <PropertyInfo> pis = StaticTool.GetPropertyInfo(typeInfo);
                if (pis.Count > 0)
                {
                    for (int i = 0; i < pis.Count; i++)
                    {
                        SetStruct(mdc, pis[i], null, i, pis.Count);
                    }
                }
                else
                {
                    List <FieldInfo> fis = StaticTool.GetFieldInfo(typeInfo);
                    if (fis.Count > 0)
                    {
                        for (int i = 0; i < fis.Count; i++)
                        {
                            SetStruct(mdc, null, fis[i], i, fis.Count);
                        }
                    }
                }
                object[] tableAttr = typeInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);//看是否设置了表特性,获取表名和表描述
                if (tableAttr != null && tableAttr.Length == 1)
                {
                    DescriptionAttribute attr = tableAttr[0] as DescriptionAttribute;
                    if (attr != null && !string.IsNullOrEmpty(attr.Description))
                    {
                        mdc.Description = attr.Description;
                    }
                }
                pis = null;
                #endregion

                if (!_ColumnCache.ContainsKey(key))
                {
                    _ColumnCache.Set(key, mdc.Clone());
                }

                return(mdc);
            }
        }