コード例 #1
0
        // This function is generic because some lists, like ILexSense.AnthroCodesRC, are lists of interfaces *derived*
        // from ICmPossibility (e.g., ICmAnthroItem). This results in type errors at compile time: parameter
        // types like IFdoReferenceCollection<ICmPossibility> don't match IFdoReferenceCollection<ICmAnthroCode>.
        // Generics solve the problem, and can be automatically inferred by the compiler to boot.
        public void SetPossibilitiesCollection <T>(IFdoReferenceCollection <T> dest, IEnumerable <T> newItems)
            where T : class, ICmPossibility
        {
            // If we know of NO valid possibility keys, don't make any changes. That's because knowing of NO valid possibility keys
            // is FAR more likely to happen because of a bug than because we really removed an entire possibility list, and if there's
            // a bug, we shouldn't drop all the FDO data for this possibility list.
            if (PossibilitiesByKey.Count == 0 && _canonicalSource == null)
            {
                return;
            }
            // We have to calculate the update (which items to remove and which to add) here; IFdoReferenceCollection won't do it for us.
            List <T>       itemsToAdd    = newItems.ToList();
            HashSet <Guid> guidsToAdd    = new HashSet <Guid>(itemsToAdd.Select(poss => poss.Guid));
            List <T>       itemsToRemove = new List <T>();

            foreach (T poss in dest)
            {
                if (!guidsToAdd.Contains(poss.Guid))
                {
                    itemsToRemove.Add(poss);
                }
            }
            dest.Replace(itemsToRemove, itemsToAdd);
        }