//========================================================================= // FunctionName : ToString /// <summary> /// 重载ToString方法输出所有列名称和对应的值 /// </summary> /// <returns>字符串</returns> //========================================================================= public override string ToString() { string className = this.GetType().ToString(); try { IDictionary typeTable = DataTypeManager.GetTypeInfo(className); if (typeTable == null) { Initialize(); } string[] columnNameList = this.GetColumnName(); StringBuilder builder = new StringBuilder(); foreach (string columnName in columnNameList) { if (builder.Length != 0) { builder.Append(", "); } builder.Append(columnName).Append("=").Append(this.GetData(columnName)); } return(builder.ToString()); } catch (Exception ex) { return(string.Format("{0}.ToString() 错误信息({1})", className, ex.Message)); } }
//========================================================================= // FunctionName : Initialize /// <summary> /// 初始化数据实体 /// </summary> /// <remarks> /// 初始化数据实体实例 /// </remarks> //========================================================================= private void Initialize() { lock (this.GetType()) { string className = this.GetType().ToString(); IDictionary typeTable = DataTypeManager.GetTypeInfo(className); if (typeTable == null) { Hashtable propertyTable = new Hashtable(); ColumnInfo infoColumn; string[] columnList = this.GetColumnName(); SqlDbType[] columnTypeList = this.GetColumnType(); string[] keyList = this.GetPrimaryKey(); string[] nullList = this.GetNullableColumn(); PropertyInfo[] properties = this.GetType().GetProperties(); for (int i = 0; i < columnList.Length; i++) { infoColumn = new ColumnInfo(); infoColumn.ColumnType = columnTypeList[i]; propertyTable[columnList[i]] = infoColumn; propertyTable[columnList[i].ToLower()] = infoColumn; } foreach (PropertyInfo infoProperty in properties) { infoColumn = (ColumnInfo)propertyTable[infoProperty.Name]; if (infoColumn == null) { continue; } infoColumn.ColumnProperty = infoProperty; if (keyList.Any(keyCol => keyCol == infoProperty.Name)) { infoColumn.IsPrimaryKey = true; } if (nullList.Any(nullCol => nullCol == infoProperty.Name)) { infoColumn.IsNullable = true; } } DataTypeManager.SetTypeInfo(className, propertyTable); } } }
//========================================================================= // FunctionName : GetColumnInfo /// <summary> /// 取得数据实体指定列的列信息 /// </summary> /// <param name="columnName">列名称</param> /// <returns>ColumnInfo类型数组</returns> /// <exception cref="BaseDataException"></exception> //========================================================================= public ColumnInfo GetColumnInfo(string columnName) { string className = this.GetType().ToString(); IDictionary typeTable = DataTypeManager.GetTypeInfo(className); if (typeTable == null) { Initialize(); } IDictionary propertyTable = (IDictionary)DataTypeManager.GetTypeInfo(className); ColumnInfo infoColumn = (ColumnInfo)propertyTable[columnName]; if (infoColumn == null) { string msg = "列名称:" + columnName; throw new Exception(); //throw new BaseDataException(MESSAGE_ID_NOT_FOUND, new string[] {msg}, null ,null ,null); } return(infoColumn); }
//========================================================================= // FunctionName : SetData /// <summary> /// 设定数据实体指定列的值 /// </summary> /// <param name="columnName">列名称</param> /// <param name="columnValue">列值</param> //========================================================================= public void SetData(string columnName, object columnValue) { string className = this.GetType().ToString(); IDictionary typeTable = DataTypeManager.GetTypeInfo(className); if (typeTable == null) { Initialize(); } IDictionary propertyTable = DataTypeManager.GetTypeInfo(className); ColumnInfo infoColumn = (ColumnInfo)propertyTable[columnName]; if (infoColumn == null) { string msg = "列名称:" + columnName; throw new Exception(); //throw new BaseDataException(MESSAGE_ID_NOT_FOUND, new string[] {msg}, null ,null ,null); } infoColumn.ColumnProperty.SetValue(this, columnValue, null); }