internal static void UpdateReference(UnifiedIMReference reference, IUnifiedIMObject host, IUnifiedIMObject target)
        {
            object hVal = reference.GetHostProperty(host);
            object tVal = reference.GetTargetProperty(target);

            if (Matches(reference, hVal, tVal))
            {
                if (typeof(IList).IsAssignableFrom(reference.SetPropertyType))
                {
                    IList list = (IList)reference.GetReferenceProperty(host);
                    if (list == null)
                    {
                        throw new ArgumentException("Reference Lists should be initiated with an instance. And never be null");
                    }
                    //if (!list.Contains(target))
                    list.Add(target);
                }
                else
                {
                    reference.SetReferenceProperty(host, target);
                }
                target.RefTo.Add(new KeyValuePair <UnifiedIMReference, IUnifiedIMObject>(reference, host));
            }
            else
            {
                if (typeof(IList).IsAssignableFrom(reference.SetPropertyType))
                {
                    IList list = (IList)reference.GetReferenceProperty(host);
                    if (list == null)
                    {
                        throw new ArgumentException("Reference Lists should be initiated with an instance. And never be null");
                    }
                    //if (list.Contains(target))
                    list.Remove(target);
                }
                else
                {
                    object curVal = reference.GetReferenceProperty(host);
                    if (curVal == target)
                    {
                        reference.SetReferenceProperty(host, null);
                    }
                }
                target.RefTo.RemoveAll(x => x.Key == reference && x.Value == host);
            }
        }
 internal static void DeleteReference(IUnifiedIMObject obj, UnifiedIMReference rf)
 {
     lock (obj.RefTo)
     {
         if (rf.TargetType == obj.DataType)
         {
             foreach (KeyValuePair <UnifiedIMReference, IUnifiedIMObject> o in obj.RefTo)
             {
                 if (o.Value.DataType == rf.HostType)
                 {
                     object sProperty = rf.GetReferenceProperty(o.Value);
                     if (typeof(IList).IsAssignableFrom(rf.SetPropertyType))
                     {
                         ((IList)sProperty).Remove(obj);
                     }
                     else if (sProperty == obj)
                     {
                         rf.SetReferenceProperty(o.Value, null);
                     }
                 }
             }
             obj.RefTo.RemoveAll(x => x.Key == rf);
         }
         else if (rf.HostType == obj.DataType)
         {
             object sProperty = rf.GetReferenceProperty(obj);
             if (typeof(IList).IsAssignableFrom(rf.SetPropertyType))
             {
                 foreach (object o in ((IList)sProperty))
                 {
                     ((IUnifiedIMObject)o).RefTo.RemoveAll(x => x.Key == rf && x.Value == obj);
                 }
                 ((IList)sProperty).Clear();
             }
             else if (sProperty == obj)
             {
                 rf.SetReferenceProperty(obj, null);
                 ((IUnifiedIMObject)sProperty).RefTo.RemoveAll(x => x.Key == rf && x.Value == obj);
             }
         }
     }
 }