Exemplo n.º 1
0
        /// <summary>
        /// Provides the implementation for operations that set member values.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member to which the value is being assigned.</param>
        /// <param name="value">The value to set to the member.</param>
        /// <returns>True if the operation is successful; otherwise, False. If this method returns False, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)</returns>
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            var normalizedKey = DynamicItem.NormalizeKey(binder.Name);

            this.Data[normalizedKey] = value;

            return(true);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Checks if the given dynamic property exists on this instance of the class.
 /// </summary>
 /// <param name="key">The name of the property to be sought.</param>
 /// <returns>True if the property name exists, otherwise False.</returns>
 public bool ContainsKey(string key)
 {
     if (null == key)
     {
         return(false);
     }
     key = DynamicItem.NormalizeKey(key);
     return(this.Data.ContainsKey(key));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the System.Dynamic.DynamicObject class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the System.Dynamic.DynamicObject class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation.</param>
        /// <returns>True if the operation is successful; otherwise, False. If this method returns False, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)</returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var normalizedKey = DynamicItem.NormalizeKey(binder.Name);

            result = null;

            if (this.Data.ContainsKey(normalizedKey))
            {
                result = this.Data[normalizedKey];
                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of DynamicItem class.
        /// </summary>
        /// <param name="items">A key/value collection representing the dynamic property names and their values, to create the class dynamic properties with.</param>
        public DynamicItem(IDictionary <string, object> items)
        {
            if (null == items)
            {
                return;
            }

            foreach (var item in items)
            {
                string normalizedKey = DynamicItem.NormalizeKey(item.Key);
                this.Data.Add(normalizedKey, item.Value);
            }
        }
Exemplo n.º 5
0
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                if (JsonToken.Null == reader.TokenType)
                {
                    return(new DynamicItem());
                }

                var jo = JObject.Load(reader);

                DynamicItem dynamicItem = new DynamicItem();

                var enumerator = jo.Properties().GetEnumerator();

                while (enumerator.MoveNext())
                {
                    var    current = enumerator.Current;
                    object value   = null;

                    if (JTokenType.Integer == current.Value.Type)
                    {
                        value = current.Value.Value <int>();
                    }
                    else if (JTokenType.Float == current.Value.Type)
                    {
                        value = current.Value.Value <float>();
                    }
                    else if (JTokenType.Guid == current.Value.Type)
                    {
                        value = current.Value.Value <Guid>();
                    }
                    else if (JTokenType.String == current.Value.Type)
                    {
                        value = current.Value.Value <string>();
                    }
                    else if (JTokenType.Boolean == current.Value.Type)
                    {
                        value = current.Value.Value <bool>();
                    }
                    else
                    {
                        value = current.Value.Value <object>();
                    }

                    dynamicItem.Data.Add(DynamicItem.NormalizeKey(current.Name), value);
                }

                return(dynamicItem);
            }
Exemplo n.º 6
0
 /// <summary>
 /// Gets or sets the property values associated with the given dynamic property name.
 /// </summary>
 /// <value>The value of the dynamic property with the given name.</value>
 public object this[string key]
 {
     get { return(this.Data[DynamicItem.NormalizeKey(key)]); }
     set { this.Data[DynamicItem.NormalizeKey(key)] = value; }
 }