예제 #1
0
 public virtual void InitializeInstance(object instance)
 {
     if (!SetupInstances.TryGetValue(instance, out _))
     {
         var subscribable = GeneralSubscribable.CreateSubscribable(instance);
         SetupInstances.Add(instance, null);
         var handler = new SubscribableHandler(this, subscribable);
         SetupInstances.Remove(instance);
         SetupInstances.Add(instance, handler);
     }
 }
예제 #2
0
        private void CachePropertyPath(string propertyPath, ISubscribable instance)
        {
            Stack <IEnumerable <string> > pathsToSubscribe = new Stack <IEnumerable <string> >();

            pathsToSubscribe.Push(propertyPath.DisassemblePropertyPath());
            var possiblePaths = PropertyDependencies.Keys.Where(key => key.StartsWith(propertyPath));
            IEnumerable <string> path;

            while (pathsToSubscribe.Count > 0)
            {
                path = pathsToSubscribe.Pop();
                // if nocache
                if (true)
                {
                    var compLength     = path.Count();
                    var reassembled    = path.ReassemblePropertyPath();
                    var nextProperties = possiblePaths.Where(pth => pth.StartsWith(reassembled))
                                         .Select(pth => pth.DisassemblePropertyPath().Skip(compLength).FirstOrDefault())
                                         .Where(pth => pth != null).Distinct();

                    object propertyValue;
                    if (reassembled == "")
                    {
                        propertyValue = instance;
                    }
                    else
                    {
                        propertyValue = instance.GetPropertyValue(reassembled);
                    }
                    var typeInfo = propertyValue?.GetType()?.GetTypeInfo();

                    var subscribableProperties = GeneralSubscribable.FilterSubscribableProperties(typeInfo, nextProperties);

                    foreach (var prop in subscribableProperties)
                    {
                        pathsToSubscribe.Push(path.Concat(new string[] { prop }));
                    }
                    if (GeneralSubscribable.CanSubscribe(typeInfo))
                    {
                        var typedPropertyValue = GeneralSubscribable.CreateSubscribable(propertyValue);
                        if (TryAddToSubscribablePropertyCache(reassembled, typedPropertyValue))
                        {
                            typedPropertyValue.PropertyChanged += GetChangeHandlerDelegate(this, reassembled);
                            if (reassembled != "" && Manager.IsTypeRegistered(propertyValue?.GetType()))
                            {
                                Manager.InitializeInstance(propertyValue);
                            }
                        }
                    }
                }
            }
        }