简体   繁体   English

字典中的值不断变化

[英]Values in Dictionary keeps changing

A quick describe aboutwhat I need to do as follows:快速描述一下我需要做的事情如下:

  • When open an document in Revit, I want to get the id and location of all elements in a Dictionary name _start_state在 Revit 中打开文档时,我想获取字典名称 _start_state 中所有元素的 ID 和位置
  • Whenever the document is changed, I want to get the id of the modified elements, then compare them to the keys in _start_state to return the original location from _start_state.每当更改文档时,我想获取修改元素的 id,然后将它们与 _start_state 中的键进行比较以从 _start_state 返回原始位置。

However, the values of the _start_state dictionary is not constant.但是,_start_state 字典的值不是常量。 Everytime CtrlApp_DocumentChanged is called(meaning an element is modified), the values (location) of the corresponding keys(ELementId)in _start_state changes.每次调用 CtrlApp_DocumentChanged(意味着修改元素)时,_start_state 中相应键(ELementId)的值(位置)都会发生变化。

        Dictionary<ElementId, Location> _start_state;
        List<ElementId> startKeys;

        public void CtrlApp_DocumentOpened(object sender, DocumentOpenedEventArgs e)
        {
            //Get the current document
            Document doc=e.Document;

            
            IEnumerable<Element> a= GetTrackedElements(doc);

            Dictionary<ElementId, Location> start_state;
            start_state = GetSnapshot(a);

            _start_state = new Dictionary<ElementId, Location>(start_state);
            
            startKeys = _start_state.Keys.ToList();

  
        }


 
         public void CtrlApp_DocumentChanged(object sender, DocumentChangedEventArgs e)        {
                       
            ICollection<ElementId> modifiedElem = e.GetModifiedElementIds();
            
            foreach (ElementId id in modifiedElem)
            {
                if (startKeys.Contains(id))//return new location instead
                {
                    Dictionary<ElementId, Location> dict = new Dictionary<ElementId, Location>();
                    List<Location> locList = new List<Location>();
                    locList.Add(_start_state[id]);
                    
                    foreach (Location loc in locList)
                    {
                        send_baseLocation(loc);
                    }

                    
                }
            }
           }

Do you suggest any way to keep the Dictionary _start_state unchanged over the time?您有什么建议可以使 Dictionary _start_state 随时间保持不变吗? I am thinking about deep cloning or ImmutableDictionary.我正在考虑深度克隆或 ImmutableDictionary。

Thanks谢谢

My guess is Location is a reference type and being mutated when document is changed.我的猜测是 Location 是一种引用类型,并且在更改文档时会发生变化。 Your only option in that case is to clone the location object yourself.在这种情况下,您唯一的选择是自己克隆位置 object。 ImmutableDictionary won't protect you from properties of Values being mutated. ImmutableDictionary 不会保护您免受 Values 属性的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM