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 ResolveReference(IUnifiedIMObject obj, UnifiedIMReference reference, Type type = null)
        {
            if (type == null)
            {
                type = obj.GetType();
            }

            if (reference.TargetType == type)
            {
                if (UseOmniBase && reference.HostProperty == "ObjectID")
                {
                    string od = (string)reference.GetTargetProperty(obj);
                    if (od != null)
                    {
                        bool             hasKey  = false;
                        IUnifiedIMObject omniObj = null;
                        lock (UnifiedSystem.OmniBase)
                        {
                            hasKey = UnifiedSystem.OmniBase.ContainsKey(od);
                            if (hasKey)
                            {
                                omniObj = OmniBase[od];
                            }
                        }
                        if (hasKey)
                        {
                            UpdateReference(reference, omniObj, obj);
                        }
                    }
                }
                else
                {
                    if (AllowIndexes && Indexes.ContainsKey(reference.HostType) && Indexes[reference.HostType].HasTypeProperty(reference.HostProperty))
                    {
                        List <IUnifiedIMObject> objs = Indexes[reference.HostType].GetIndex(reference.HostProperty, reference.GetTargetProperty(obj));
                        foreach (object o in objs)
                        {
                            UpdateReference(reference, (IUnifiedIMObject)o, obj);
                        }
                    }
                    else
                    if (_databaseGetters.ContainsKey(reference.HostType))
                    {
                        foreach (object o in _databaseGetters[reference.HostType])
                        {
                            UpdateReference(reference, (IUnifiedIMObject)o, obj);
                        }
                    }
                }
            }
            else if (reference.HostType == type)
            {
                if (UseOmniBase && reference.TargetProperty == "ObjectID")
                {
                    string od = (string)reference.GetHostProperty(obj);
                    if (od != null)
                    {
                        bool             hasKey  = false;
                        IUnifiedIMObject omniObj = null;
                        lock (UnifiedSystem.OmniBase)
                        {
                            hasKey = UnifiedSystem.OmniBase.ContainsKey(od);
                            if (hasKey)
                            {
                                omniObj = OmniBase[od];
                            }
                        }
                        if (hasKey)
                        {
                            UpdateReference(reference, obj, omniObj);
                        }
                    }
                }
                else
                {
                    if (AllowIndexes && Indexes.ContainsKey(reference.TargetType) && Indexes[reference.TargetType].HasTypeProperty(reference.TargetProperty))
                    {
                        List <IUnifiedIMObject> objs = Indexes[reference.TargetType].GetIndex(reference.TargetProperty, reference.GetHostProperty(obj));
                        foreach (object o in objs)
                        {
                            UpdateReference(reference, obj, (IUnifiedIMObject)o);
                        }
                    }
                    else if (_databaseGetters.ContainsKey(reference.TargetType))
                    {
                        foreach (object o in _databaseGetters[reference.TargetType])
                        {
                            UpdateReference(reference, obj, (IUnifiedIMObject)o);
                        }
                    }
                }
            }
        }