コード例 #1
0
        /// <summary>
        /// Enumerate the properties of an object, casting to type T
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static IEnumerable <T> Enumerate <T>(object obj, IEnumerable <Type> ignoreAttributes)
        {
            HashSet <Type> IgnoreList = new HashSet <Type>();

            if (ignoreAttributes != null)
            {
                IgnoreList.AddRange(ignoreAttributes);
            }

            IDictionary <string, object> source;

            if (obj is IDictionary <string, object> )
            {
                source = (IDictionary <string, object>)obj;
            }
            else
            {
                source = Objects.ToExpando <JsObject>(obj, false, ignoreAttributes);
            }
            foreach (KeyValuePair <string, object> kvp in source)
            {
                if (typeof(T) == typeof(KeyValuePair <string, object>))
                {
                    yield return((T)(object)(new KeyValuePair <string, object>(kvp.Key,
                                                                               kvp.Value is IDictionary <string, object>?
                                                                               ToExpando((IDictionary <string, object>)kvp.Value) :
                                                                                   kvp.Value)));
                }
                else
                {
                    yield return(Objects.Convert <T>(kvp.Value));
                }
            }
        }
コード例 #2
0
ファイル: JsObject.cs プロジェクト: mburgman101/CsQuery
        protected bool TryGetMember(string name, Type type, out object result)
        {
            object value   = null;
            bool   success = String.IsNullOrEmpty(name) ?
                             false :
                             InnerProperties.TryGetValue(name, out value);

            if (!success)
            {
                if (AllowMissingProperties)
                {
                    if (type == typeof(object))
                    {
                        result = MissingPropertyValue;
                    }
                    else
                    {
                        result = Objects.DefaultValue(type);
                    }
                    success = true;
                }
                else
                {
                    throw new KeyNotFoundException("There is no property named \"" + name + "\".");
                }
            }
            else
            {
                if (type == typeof(object))
                {
                    result = value;
                }
                else
                {
                    result = Objects.Convert(value, type);
                }
            }
            return(success);
        }
コード例 #3
0
        /// <summary>
        /// Get the current value of the first element in the set of matched elements, and try to convert
        /// to the specified type.
        /// </summary>
        ///
        /// <typeparam name="T">
        /// The type to which the value should be converted.
        /// </typeparam>
        ///
        /// <returns>
        /// A value or object of type T.
        /// </returns>
        ///
        /// <url>
        /// http://api.jquery.com/val/#val1
        /// </url>

        public T Val <T>()
        {
            string val = Val();

            return(Objects.Convert <T>(val));
        }