- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2010-12-24 14:00:06
|
显示全部楼层
本帖最后由 lzh741206 于 2010-12-24 14:35 编辑
虽然字典的组织像一个Tree,但它确实不是以Tree的形式储存的
下面的代码是的TlsBasal里关于字典的部分操作,你可以看看
另外,父节点可以通过打开Id后获取实体的OwnerId属性获取
- /// <summary>
- /// 获取子字典
- /// </summary>
- /// <param name="dict">根字典</param>
- /// <param name="createSubDictionary">是否创建子字典</param>
- /// <param name="dictNames">键值列表</param>
- /// <returns></returns>
- public DBDictionary GetSubDictionary(DBDictionary dict, bool createSubDictionary, params string[] dictNames)
- {
- if (createSubDictionary)
- {
- using (dict.UpgradeOpenAndRun())
- {
- dict.TreatElementsAsHard = true;
- }
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- dict = GetObject<DBDictionary>(dict.GetAt(name));
- }
- else
- {
- DBDictionary subDict = new DBDictionary();
- dict.SetAt(name, subDict);
- Transaction.AddNewlyCreatedDBObject(subDict, true);
- dict = subDict;
- dict.TreatElementsAsHard = true;
- }
- }
- }
- else
- {
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- dict = GetObject<DBDictionary>(dict.GetAt(name));
- }
- else
- {
- return null;
- }
- }
- }
- return dict;
- }
- //保存和获取数据
- #region Value
- /// <summary>
- /// 保存数据到字典
- /// </summary>
- /// <param name="value">数据</param>
- /// <param name="dict">字典</param>
- /// <param name="key">键值</param>
- public void SetToDictionary(DBObject value, DBDictionary dict, string key)
- {
- if (dict.Contains(key))
- dict.Remove(key);
- dict.SetAt(Transaction, key, value);
- }
- /// <summary>
- /// 从字典中获取数据
- /// </summary>
- /// <param name="dict">字典</param>
- /// <param name="key">键值</param>
- /// <returns>数据</returns>
- public DBObject GetFromDictionary(DBDictionary dict, string key)
- {
- if (dict != null)
- {
- if (dict.Contains(key))
- {
- DBObject obj = GetObject(dict.GetAt(key));
- return obj;
- }
- }
- return null;
- }
- /// <summary>
- /// 保存数据到实体字典
- /// </summary>
- /// <param name="value">数据</param>
- /// <param name="obj">图元实体</param>
- /// <param name="keys">键值列表</param>
- public void SetToDictionary(DBObject value, DBObject obj, params string[] keys)
- {
- string key;
- var dictnames = GetDictNames(keys, out key);
- ObjectId id = obj.ExtensionDictionary;
- if (id == ObjectId.Null)
- {
- using (obj.UpgradeOpenAndRun())
- {
- obj.CreateExtensionDictionary();
- }
- id = obj.ExtensionDictionary;
- }
- DBDictionary dict = GetSubDictionary(id, true, dictnames);
- SetToDictionary(value, dict, key);
- }
- /// <summary>
- /// 保存数据到命名字典
- /// </summary>
- /// <param name="value">数据</param>
- /// <param name="keys">键值列表</param>
- public void SetToDictionary(DBObject value, params string[] keys)
- {
- string key;
- var dictnames = GetDictNames(keys, out key);
- DBDictionary dict = GetSubDictionary(Database.NamedObjectsDictionaryId, true, dictnames);
- SetToDictionary(value, dict, key);
- }
- /// <summary>
- /// 从对象字典中获取数据
- /// </summary>
- /// <param name="obj">图元实体</param>
- /// <param name="keys">键值列表</param>
- /// <returns>数据</returns>
- public DBObject GetFromDictionary(DBObject obj, params string[] keys)
- {
- string key;
- var dictnames = GetDictNames(keys, out key);
- ObjectId id = obj.ExtensionDictionary;
- DBDictionary dict = GetSubDictionary(id, false, dictnames);
- return GetFromDictionary(dict, key);
- }
- /// <summary>
- /// 从命名字典中获取数据
- /// </summary>
- /// <param name="keys">键值列表</param>
- /// <returns>数据</returns>
- public DBObject GetFromDictionary(params string[] keys)
- {
- string key;
- var dictnames = GetDictNames(keys, out key);
- DBDictionary dict = GetSubDictionary(Database.NamedObjectsDictionaryId, false, dictnames);
- return GetFromDictionary(dict, key);
- }
- #endregion
|
|