/// <summary> /// 添加字段 /// </summary> /// <param name="bindObject">执行对象</param> /// <param name="field">字段</param> /// <param name="tag">tag</param> static void AddField(object bindObject, string field, TablePropertyTag tag) { lock (lockObject) if (!FieldIsExist(bindObject, field) && ObjectIsExist(bindObject)) { pool[bindObject].Add(field, tag); } }
/// <summary> /// 自动绑定读表 /// </summary> /// <param name="bindObject">执行对象</param> public static void Bind(object bindObject) { lock (lockObject) { DetectionAttribute(bindObject); Type type = bindObject.GetType(); foreach (string fieldName in pool[bindObject].Keys) { TablePropertyTag tag = pool[bindObject][fieldName]; if (!string.IsNullOrEmpty(tag.tableName) && !string.IsNullOrEmpty(tag.primaryKey) && !string.IsNullOrEmpty(tag.key)) { string value = MyTable.GetInstance().GetValue(tag.tableName, tag.primaryKey, tag.key); FieldInfo info = type.GetField(fieldName); info.SetValue(bindObject, TypeDescriptor.GetConverter(info.FieldType).ConvertFrom(value)); } } } }
/// <summary> /// 更新Tag /// </summary> /// <param name="bindObject">执行对象</param> /// <param name="field">更新字段</param> /// <param name="parameters">更新参数</param> public static void UpdateTag(object bindObject, string field, params string[] parameters) { lock (lockObject) { if (!ObjectIsExist(bindObject)) { AddObject(bindObject); } if (!FieldIsExist(bindObject, field)) { AddField(bindObject, field, new TablePropertyTag()); } TablePropertyTag tag = pool[bindObject][field]; for (int i = 0; i < parameters.Length; i += 2) { FieldInfo info = tag.GetType().GetField(parameters[i]); info.SetValue(tag, parameters[i + 1]); } } }