Exemplo n.º 1
0
        private static Property ParseProperty(object root, string propertyName)
        {
            try
            {
                if (root == null)
                {
                    return(null);
                }
                string[] items = propertyName.Split('.');
                if (items == null || items.Length < 1)
                {
                    return(null);
                }
                //单个属性
                if (items.Length == 1)
                {
                    return(new Property(root.GetType().GetProperty(propertyName, propertyBindingFlags), root));
                }
                //多个属性
                object target = root;

                string name = null;
                for (int i = 0; i < items.Length; i++)
                {
                    name = items[i];
                    //依据属性名称获取对象的属性
                    PropertyInfo p = target.GetType().GetProperty(name, propertyBindingFlags);
                    //取最后一个即属性的本身(过滤了该属性的基类)
                    if (i < items.Length - 1)
                    {
                        var oldTarget = target;
                        target = p.Get(target);//p.GetValue(target, null);
                        if (target == null)
                        {
                            if (!p.CanWrite)
                            {
                                return(null);
                            }
                            target = ObjectCreator.Create(p.PropertyType);
                            p.Set(oldTarget, target);
                        }
                    }
                }
                return(new Property(target.GetType().GetProperty(name, propertyBindingFlags), target));
            }
            catch
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        /// <param name="index"></param>
        public static void SetProperty(this object obj, string propertyName, object value, params object[] index)
        {
            Guard.NotNull(obj, "obj");
            Guard.NotNull(propertyName, "propertyName");

            Property p = ParseProperty(obj, propertyName);

            if (p == null || p.Name == null)
            {
                return;
            }

            if (!p.Name.CanWrite)
            {
                return;
            }

            if (value == null)
            {
                if (p.Name.PropertyType.IsValueType)
                {
                    value = ObjectCreator.Create(p.Name.PropertyType);
                }
            }
            try
            {
                if (!p.Name.PropertyType.IsAssignableFrom(value.GetType()))
                {
                    value = Mapper.Map(value, value.GetType(), p.Name.PropertyType);
                }
                if (p.Owner.GetType().IsValueType || (index != null && index.Length > 0))
                {
                    p.Name.SetValue(p.Owner, value, index);
                }
                else
                {
                    p.Name.ToMemberSetter()(p.Owner, value);
                }
            }
            finally { }
        }
Exemplo n.º 3
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="type"></param>
        ///// <returns></returns>
        //public static Type GetNonNullableType(Type type)
        //{
        //    if (TypeHelper.IsNullable(type))
        //        return Nullable.GetUnderlyingType(type);
        //    return type;
        //}

        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object GetDefault(Type type)
        {
            //Guard.NotNull(type, "type");
            return(ObjectCreator.Create(type));
        }