Exemplo n.º 1
0
        public void Get_setter_method_for_simple_properties()
        {
            Type                type         = typeof(Test);
            PropertyInfo        propertyInfo = type.GetProperty("TestProperty");
            SetPropertyDelegate setMethod    = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
            Test                test         = new Test();

            setMethod.Invoke(test, "test");
            Assert.AreEqual("test", test.TestProperty);
        }
        public void Get_setter_method_for_dictionary_properties()
        {
            var  dict = new Dictionary <string, string>();
            Type type = typeof(Dictionary <string, string>);

            foreach (var propertyInfo in type.GetProperties())
            {
                SetPropertyDelegate setMethod = JsvDeserializeType.GetSetPropertyMethod(type, propertyInfo);
                if (setMethod == null)
                {
                    continue;
                }
                Console.WriteLine(propertyInfo.Name);
                setMethod.Invoke(dict, propertyInfo.Name);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 设置目标对象的属性值
        /// </summary>
        /// <param name="target">目标对象</param>
        /// <param name="value">需要设置的值</param>
        public void Set(object target, object value)
        {
            if (_PropertyInfo.CanWrite)
            {
                if (_SetDelegate == null)
                {
                    _SetDelegate = setPropertyValue(_TargetType, _PropertyInfo);
                }
                //先把值转换为对应的数据类型
                object cVal = MB.Util.MyReflection.Instance.ConvertValueType(_PropertyInfo.PropertyType, value);

                _SetDelegate.Invoke(target, cVal);
            }
            else
            {
                throw new DynamicPropertyAccessorException(
                          string.Format("属性 \"{0}\" 不存在一个SET 方法.",
                                        _PropertyInfo.Name));
            }
        }