Ejemplo n.º 1
0
        }     // end DynObject

        /// <summary>
        /// Attempts to get the requested member as a key in our wrapped dictionary. If the key does not exist, we first
        /// try accessing the member as a member of the Dictionary class. Failing that, we set it on the dictionary, and
        /// return a new DynObject, wrapping a dictionary.
        /// </summary>
        /// <param name="binder">The GetMemberBinder containing information about the member we're attempting to access.</param>
        /// <param name="result">The results of calling/accessing the member.</param>
        /// <returns>Returns true if we successfully called or accessed the member, false otherwise.</returns>
        public override bool TryGetMember(GetMemberBinder binder, out Object result)
        {
            if (wrappedObject.ContainsKey(binder.Name))
            {
                try
                {
                    // Attempt to access the member on our internal dict.
                    result = Wrap(wrappedObject[binder.Name]);
                    return(true);
                }
                catch
                {
                    // Something happened, and we failed to retrieve the key.
                    result = null;
                    return(false);
                } // end try/catch
            }
            else
            {
                try
                {
                    // Attempt to access internal members
                    result = wrappedObject.GetType().GetProperty(binder.Name).GetValue(wrappedObject, null);
                    return(true);
                }
                catch
                {
                    // There's no member by that name, so, set a new key in the dictionary.
                    // This will make nesting easier.
                    result = new DynObject();
                    wrappedObject[binder.Name] = result;
                    return(true);
                } // end try/catch
            }     // end if
        }         // end TryGetMember
Ejemplo n.º 2
0
        } // end GetEnumerator

        IEnumerator <object> IEnumerable <object> .GetEnumerator()
        {
            foreach (object obj in wrappedObject)
            {
                yield return(DynObject.Wrap(obj));
            } // end foreach
        }     // end GetEnumerator
Ejemplo n.º 3
0
        }     // end DynList

        #region DynList Overrides

        /// <summary>
        /// Accessor method of members of the list. Returns those objects wrapped by DynObject.
        /// </summary>
        /// <param name="index">The index of the time we're retrieving.</param>
        /// <returns>The object, as returned by DynObject.Wrap.</returns>
        public object this[int index]
        {
            get
            {
                return(DynObject.Wrap(wrappedObject[index]));
            } // end get
            set
            {
                // If we're trying to set DynList or DynObject, we really should only set the underlying object.
                // This really helps keep things clean, and serializable.
                if (value is DynObject)
                {
                    wrappedObject[index] = ((DynObject)value).wrappedObject;
                }
                else if (value is DynList)
                {
                    wrappedObject[index] = ((DynList)value).wrappedObject;
                }
                else
                {
                    wrappedObject[index] = value;
                } // end if
            }     // end set
        }         // end object this[]