Exemplo n.º 1
0
        /// <summary>
        /// 获得指定类型的数据表映射信息对象
        /// </summary>
        /// <param name="ObjectType">对象类型</param>
        /// <returns>获得的映射信息对象</returns>
        /// <remarks>
        /// 本函数内部使用了 myBufferedInfos 来缓存信息,提高性能。
        /// </remarks>
        private TableBindInfo GetBindInfo(Type ObjectType)
        {
            if (ObjectType == null)
            {
                throw new ArgumentNullException("OjbectType");
            }
            // 查找已缓存的映射信息对象
            TableBindInfo info = ( TableBindInfo )myBufferedInfos[ObjectType];

            if (info != null)
            {
                return(info);
            }

            // 若未找到则创建新的映射信息对象
            BindTableAttribute ta = ( BindTableAttribute )System.Attribute.GetCustomAttribute(
                ObjectType, typeof(BindTableAttribute));

            if (ta == null)
            {
                return(null);
            }

            TableBindInfo NewInfo = new TableBindInfo();

            NewInfo.ObjectType = ObjectType;
            NewInfo.Attribute  = ta;
            NewInfo.TableName  = ta.Name;
            if (NewInfo.TableName == null || NewInfo.TableName.Trim().Length == 0)
            {
                // 若在特性中没有指明绑定的表名则使用默认的对象类型名称
                NewInfo.TableName = ObjectType.Name;
            }
            System.Text.StringBuilder    myFieldList = new System.Text.StringBuilder();
            System.Collections.ArrayList fields      = new System.Collections.ArrayList();
            // 遍历所有的公开的实例属性来获得字段绑定信息
            foreach (System.Reflection.PropertyInfo p in ObjectType.GetProperties(
                         System.Reflection.BindingFlags.Instance
                         | System.Reflection.BindingFlags.Public
                         ))
            {
                BindFieldAttribute fa = ( BindFieldAttribute )Attribute.GetCustomAttribute(
                    p, typeof(BindFieldAttribute));
                if (fa != null)
                {
                    FieldBindInfo NewFieldInfo = new FieldBindInfo();
                    NewFieldInfo.Attribute = fa;
                    NewFieldInfo.FieldName = fa.Name;
                    if (NewFieldInfo.FieldName == null || NewFieldInfo.FieldName.Trim().Length == 0)
                    {
                        // 若在特性中没有指明绑定的字段名则使用默认的属性名称
                        NewFieldInfo.FieldName = p.Name;
                    }
                    if (myFieldList.Length > 0)
                    {
                        myFieldList.Append(",");
                    }
                    myFieldList.Append(FixFieldName(NewFieldInfo.FieldName));
                    NewFieldInfo.Property     = p;
                    NewFieldInfo.ValueType    = p.PropertyType;
                    NewFieldInfo.DefaultValue = GetDefaultValue(p);
                    fields.Add(NewFieldInfo);
                }
            }
            NewInfo.Fields        = ( FieldBindInfo[])fields.ToArray(typeof(FieldBindInfo));
            NewInfo.FieldNameList = myFieldList.ToString();
            // 缓存绑定信息对象
            myBufferedInfos[ObjectType] = NewInfo;
            return(NewInfo);
        }
Exemplo n.º 2
0
        /// <summary>
        /// ���ָ�����͵����ݱ�ӳ����Ϣ����
        /// </summary>
        /// <param name="ObjectType">��������</param>
        /// <returns>��õ�ӳ����Ϣ����</returns>
        /// <remarks>
        /// �������ڲ�ʹ���� myBufferedInfos ��������Ϣ��������ܡ�
        /// </remarks>
        private TableBindInfo GetBindInfo( Type ObjectType )
        {
            if( ObjectType == null )
            {
                throw new ArgumentNullException("OjbectType");
            }
            // �����ѻ����ӳ����Ϣ����
            TableBindInfo info = ( TableBindInfo ) myBufferedInfos[ ObjectType ] ;
            if( info != null )
            {
                return info ;
            }

            // ��δ�ҵ��򴴽��µ�ӳ����Ϣ����
            BindTableAttribute ta = ( BindTableAttribute ) System.Attribute.GetCustomAttribute(
                ObjectType , typeof( BindTableAttribute ));
            if( ta == null )
            {
                return null;
            }

            TableBindInfo NewInfo = new TableBindInfo();
            NewInfo.ObjectType = ObjectType ;
            NewInfo.Attribute = ta ;
            NewInfo.TableName = ta.Name ;
            if( NewInfo.TableName == null || NewInfo.TableName.Trim().Length == 0 )
            {
                // ����������û��ָ���󶨵ı�����ʹ��Ĭ�ϵĶ�����������
                NewInfo.TableName = ObjectType.Name ;
            }
            System.Text.StringBuilder myFieldList = new System.Text.StringBuilder();
            System.Collections.ArrayList fields = new System.Collections.ArrayList();
            // �������еĹ�����ʵ������������ֶΰ���Ϣ
            foreach( System.Reflection.PropertyInfo p in ObjectType.GetProperties(
                System.Reflection.BindingFlags.Instance
                | System.Reflection.BindingFlags.Public
                ))
            {
                BindFieldAttribute fa = ( BindFieldAttribute ) Attribute.GetCustomAttribute(
                    p , typeof( BindFieldAttribute ));
                if( fa != null )
                {
                    FieldBindInfo NewFieldInfo = new FieldBindInfo();
                    NewFieldInfo.Attribute = fa ;
                    NewFieldInfo.FieldName = fa.Name ;
                    if( NewFieldInfo.FieldName == null || NewFieldInfo.FieldName.Trim().Length == 0 )
                    {
                        // ����������û��ָ���󶨵��ֶ�����ʹ��Ĭ�ϵ���������
                        NewFieldInfo.FieldName = p.Name ;
                    }
                    if( myFieldList.Length > 0 )
                    {
                        myFieldList.Append(",");
                    }
                    myFieldList.Append( FixFieldName( NewFieldInfo.FieldName ) ) ;
                    NewFieldInfo.Property = p ;
                    NewFieldInfo.ValueType = p.PropertyType ;
                    NewFieldInfo.DefaultValue = GetDefaultValue( p );
                    fields.Add( NewFieldInfo );
                }
            }
            NewInfo.Fields = ( FieldBindInfo[]) fields.ToArray( typeof( FieldBindInfo ));
            NewInfo.FieldNameList = myFieldList.ToString();
            // �������Ϣ����
            myBufferedInfos[ ObjectType ] = NewInfo ;
            return NewInfo ;
        }