Exemplo n.º 1
0
        public void CanSetAndGetPublicPropertyNoGeneric()
        {
            var a     = new A();
            var seter = ReflectionUtils.BuildSetter(typeof(A).GetProperty("IntProp"));
            var geter = ReflectionUtils.BuildGetter(typeof(A).GetProperty("IntProp"));

            seter(a, 2);
            Assert.AreEqual(2, a.IntProp);
            var x = geter(a);

            Assert.AreEqual(2, x);
        }
Exemplo n.º 2
0
        public void CanSetAndNonPublicPropertyNoGeneric()
        {
            var a     = new A();
            var seter = ReflectionUtils.BuildSetter(typeof(A).GetProperty("IntProp2", BindingFlags.NonPublic | BindingFlags.Instance));
            var geter = ReflectionUtils.BuildGetter(typeof(A).GetProperty("IntProp2", BindingFlags.NonPublic | BindingFlags.Instance));

            seter(a, 2);
            Assert.AreEqual(2, a.IntProp2);
            var x = geter(a);

            Assert.AreEqual(2, x);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Converts object properties and values to dictionary
        /// </summary>
        /// <param name="obj"> </param>
        /// <param name="existed"></param>
        /// <param name="nullsafe"></param>
        /// <param name="itemdelimiter"></param>
        /// <param name="valdelimiter"></param>
        /// <param name="escapechar"></param>
        /// <param name="trim"></param>
        /// <param name="urlescape"></param>
        /// <returns> </returns>
        public static IDictionary <string, object> ToDict(this object obj, IDictionary <string, object> existed = null, bool nullsafe = true, char itemdelimiter = ';', char valdelimiter = '=', char escapechar = '\\', bool trim = true, bool urlescape = false)
        {
            if (null == obj)
            {
                if (nullsafe)
                {
                    return(new Dictionary <string, object>());
                }
                return(null);
            }

            var result = existed ?? new Dictionary <string, object>();
            var str    = obj as string;

            if (null != str)
            {
                ReadAsDictionary(str, itemdelimiter, valdelimiter, trim, escapechar, urlescape, result);
                return(result);
            }

            var convertible = obj as IConvertible;

            if (null != convertible)
            {
                result[DictValueName] = convertible;
            }

            var collection = obj as ICollection;

            if (null != collection)
            {
                if (CheckDictionary <string, object>(obj, result))
                {
                    return(result);
                }
                if (CheckDictionary <string, string>(obj, result))
                {
                    return(result);
                }
                if (ReflectionUtils.IsGenericCompatible <IEnumerable <KeyValuePair <object, object> > >(collection))
                {
                    Func <object, object> keyGetter = null;
                    Func <object, object> valGetter = null;
                    foreach (var pair in collection)
                    {
                        if (null == keyGetter)
                        {
                            keyGetter = ReflectionUtils.BuildGetter(pair.GetType().GetProperty("Key"));
                            valGetter = ReflectionUtils.BuildGetter(pair.GetType().GetProperty("Value"));
                        }
                        var key = keyGetter(pair);
                        var val = valGetter(pair);
                        result[key.ToString()] = val;
                    }
                }
                else
                {
                    var idx = 0;
                    foreach (var item in collection)
                    {
                        result[idx.ToString()] = item;
                        idx++;
                    }
                }

                return(result);
            }

            var type = obj.GetType();

            foreach (var p in type.GetProperties())
            {
                //result.Add(p.Name, ReflectionUtils.BuildGetter(p)(obj));
                result[p.Name] = p.GetValue(obj);
            }
            return(result);
        }