用XDATA不是更简单
你的扩展数据用法有问题
贴个相关的程序你看看
- public static class DBDictionaryEx
- {
- public static IEnumerable GetAllObjects(this DBDictionary dict, Transaction tr) where T : DBObject
- {
- foreach (DBDictionaryEntry e in dict)
- {
- yield return
- tr.GetObject(e.Value, OpenMode.ForRead) as T;
- }
- }
-
- public static T GetAt(this DBDictionary dict, Transaction tr, string key) where T : DBObject
- {
- if (dict.Contains(key))
- {
- ObjectId id = dict.GetAt(key);
- if (!id.IsNull)
- {
- return tr.GetObject(id, OpenMode.ForRead) as T;
- }
- }
- return null;
- }
- public static void SetAt(this DBDictionary dict, Transaction tr, string key, T obj) where T : DBObject
- {
- using (dict.UpgradeOpenAndRun())
- {
- dict.SetAt(key, obj);
- tr.AddNewlyCreatedDBObject(obj, true);
- }
- }
- #region GetSubDictionary
- internal static List GetDictNames(string[] keys, out string key)
- {
- List dictNames = new List(keys);
- if (dictNames.Count > 0)
- {
- int index = dictNames.Count - 1;
- key = dictNames[index];
- dictNames.RemoveAt(index);
- }
- else
- {
- key = "*";
- }
- return dictNames;
- }
- internal static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, IEnumerable dictNames)
- {
- Database db = dict.Database;
- DBDictionary subdict;
- if (createSubDictionary)
- {
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- subdict = dict.GetAt(name).Open();
- dict.Close();
- dict.Dispose();
- dict = subdict;
- }
- else
- {
- using (dict.UpgradeOpenAndRun())
- {
- subdict = new DBDictionary();
- dict.SetAt(name, subdict);
- db.AddDBObject(subdict);
- }
- dict.Close();
- dict.Dispose();
- dict = subdict;
- }
- }
- }
- else
- {
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- subdict = dict.GetAt(name).Open();
- dict.Close();
- dict.Dispose();
- dict = subdict;
- }
- else
- return null;
- }
- }
- return dict;
- }
- public static DBDictionary GetSubDictionary(this DBDictionary dict, bool createSubDictionary, params string[] dictNames)
- {
- return
- GetSubDictionary(
- dict,
- createSubDictionary,
- (IEnumerable)dictNames);
- }
- internal static DBDictionary GetSubDictionary(this DBDictionary dict, Transaction tr, bool createSubDictionary, IEnumerable dictNames)
- {
- if (createSubDictionary)
- {
- using (dict.UpgradeOpenAndRun())
- dict.TreatElementsAsHard = true;
- foreach (string name in dictNames)
- {
- if (dict.Contains(name))
- {
- dict = dict.GetAt(name).GetObject(tr);
- }
- else
- {
- DBDictionary subDict = new DBDictionary();
- dict.SetAt(tr, name, subDict);
|