Esempio n. 1
0
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            result = null;
            switch (binder.Name)
            {
            case "IsCollection":
            {
                result = this.Elements.Count() > 0;
                return(true);
            }

            case "GetProperties":
            {
                result = this.propertiesSource.GetProperties();
                return(true);
            }

            case "GetPropertiesWith":
            {
                if (args.Length == 2 &&
                    args[0].GetType() == typeof(String) &&
                    args[1].GetType() == typeof(String))
                {
                    result = this.propertiesSource.GetProperties(args[0].ToString(), args[1].ToString());
                    return(true);
                }
                break;
            }

            case "GetExtendedPropertyValue":
            {
                if (args.Length == 2 &&
                    args[0].GetType() == typeof(String) &&           //Property Name
                    args[1].GetType() == typeof(String))             //Extended Property Name
                {
                    result = this.propertiesSource.GetExtendedPropertyValue(args[0].ToString(), args[1].ToString());
                    return(true);
                }
                break;
            }

            case "SetExtendedPropertyValue":
            {
                if (args.Length == 3 &&
                    args[0].GetType() == typeof(String) &&           //Property Name
                    args[1].GetType() == typeof(String) &&           //Extended Property Name
                    args[2].GetType() == typeof(String))             //Extended Property Value
                {
                    result = true;
                    this.propertiesSource.SetExtendedPropertyValue(args[0].ToString(), args[1].ToString(), args[2].ToString());
                    return(true);
                }
                break;
            }

            case "Import":
            {
                //Check parameters
                if (args.Length == 2 &&
                    args[0].GetType() == typeof(ConfigElement) &&
                    args[1].GetType() == typeof(String))
                {
                    var element = Elements.Import(args[0] as ConfigElement, args[1].ToString());
                    element.Id = args[1].ToString();
                    cachedElements.Add(args[1].ToString(), element);
                    result = element;
                    return(true);
                }
                break;
            }

            case "Add":
            {
                //Check parameters
                if (args.Length == 1 &&
                    args[0].GetType() == typeof(String))
                {
                    var element = Elements.Add(args[0].ToString());
                    element.Id = args[0].ToString();
                    cachedElements.Add(args[0].ToString(), element);
                    result = element;
                    if (CollectionChanged != null)
                    {
                        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, result));
                    }

                    return(true);
                }
                break;
            }

            case "Append":
            {
                //Assumes we are appending to a collection.
                if (args.Length == 2 &&
                    args[0].GetType() == typeof(String) &&
                    args[1].GetType() == typeof(ConfigElement) &&
                    !string.IsNullOrEmpty(GetProperty("CollectionOf")))
                {
                    var element = args[1] as ConfigElement;
                    Elements.Append(args[0].ToString(), element);
                    element.Id = args[0].ToString();
                    cachedElements.Add(args[0].ToString(), element);
                    result = element;
                    if (CollectionChanged != null)
                    {
                        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, result));
                    }

                    return(true);
                }
                break;
            }

            case "AddCollection":
            {
                //Check parameters
                if (args.Length == 2 &&
                    args[0].GetType() == typeof(String) &&
                    args[1].GetType() == typeof(String) &&
                    string.IsNullOrEmpty(CollectionOf))          //Dont want to add to a collection level element
                {
                    var collection = Elements.AddCollection(args[0].ToString(), args[1].ToString());
                    collection.Id = args[0].ToString();

                    cachedElements.Add(args[0].ToString(), collection);
                    result = collection;
                    return(true);
                }
                break;
            }

            case "Remove":
            {
                //Check parameters
                if (args.Length == 1 &&
                    args[0].GetType() == typeof(String))
                {
                    result = Elements.Remove(args[0].ToString());
                    if (cachedElements.ContainsKey(args[0].ToString()))
                    {
                        cachedElements.Remove(args[0].ToString());
                    }

                    if (CollectionChanged != null)
                    {
                        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
                    }

                    return(true);
                }
                break;
            }

            case "RemoveCollection":
            {
                //Check parameters
                if (args.Length == 1 &&
                    args[0].GetType() == typeof(String) &&
                    string.IsNullOrEmpty(CollectionOf))          //Dont want to add to a collection level element
                {
                    if (cachedElements.ContainsKey(args[0].ToString()))
                    {
                        cachedElements.Remove(args[0].ToString());
                    }
                    result = Elements.RemoveCollection(args[0].ToString());
                    return(true);
                }
                break;
            }
            }
            return(false);
        }