Example #1
0
        public static void ClearAllBindings(object targetObjectOrType)
        {
            if (targetObjectOrType == null)
            {
                throw new ArgumentNullException("targetObjectOrType");
            }

            lock (instances)
            {
                int hashCode = targetObjectOrType.GetHashCode();
                foreach (var key in instances.Keys.Where(kvp => kvp.Value == hashCode).ToList())
                {
                    var list = instances[key];
                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        DumbBinding binding = list[i];
                        if (object.Equals(targetObjectOrType, binding.Target.TargetObject) || binding.Target.TargetObject == null)
                        {
                            binding.Clear();
                            list.RemoveAt(i);
                        }
                    }

                    if (list.Count == 0)
                    {
                        instances.Remove(key);
                    }
                }
            }
        }
Example #2
0
        public static void ClearBinding(DumbBindingTarget target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            object targetObject = target.TargetObject;

            if (targetObject == null)
            {
                throw new ArgumentException("target.TargetObject is no longer alive.", "target");
            }

            lock (instances)
            {
                KeyValuePair <int, int> key = new KeyValuePair <int, int>(target.GetHashCode(), targetObject.GetHashCode());
                if (instances.ContainsKey(key))
                {
                    var list = instances[key];
                    for (int i = list.Count - 1; i >= 0; i--)
                    {
                        DumbBinding binding = list[i];
                        if (object.Equals(target, binding.Target) || binding.Target.TargetObject == null)
                        {
                            binding.Clear();
                            list.RemoveAt(i);
                        }
                    }

                    if (list.Count == 0)
                    {
                        instances.Remove(key);
                    }
                }
            }
        }