Exemple #1
0
        /// <summary>
        /// 设置指定字段的值.
        /// </summary>
        /// <param name="propertyName">属性名称.</param>
        /// <param name="Val">属性值.</param>
        /// <param name="requiredConvert">若需进行数据类型转换则为 <c>true</c>,否则应为 <c>false</c>.</param>
        public void SetValue(string propertyName, object Val, bool requiredConvert = false)
        {
            // 首先获得属性成员信息,以便进行属性值的数据有效性验证.
            Type         typeMe = this.GetType();
            PropertyInfo pi     = typeMe.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);

            if (pi == null)
            {
                return;
            }
            // 获得得用于验证属性值的有效性的特性信息.
            EntityPropertyAttribute attribute = pi.GetCustomAttribute(typeof(EntityPropertyAttribute)) as EntityPropertyAttribute;

            if (attribute == null) // 若属性未指定相关的特征,则创建默认的数据有效性特征.
            {
                attribute = new EntityPropertyAttribute();
            }
            // 进行属性值的数据类型转换.
            object propertyValue; // 属性最终的值.

            if (Val == null || Val == DBNull.Value)
            {
                if (!(attribute.AllowNull))
                {
                    throw new NotSupportedException(string.Format("属性 {0} 不允许空值.", propertyName));
                }
                propertyValue = attribute.DefaultValue;
                if (propertyValue == null)
                {
                    propertyValue = pi.PropertyType.IsValueType ? Activator.CreateInstance(pi.PropertyType) : null;
                }
            }
            else
            {
                if (requiredConvert)
                {
                    if (pi.PropertyType == typeof(string))
                    {
                        propertyValue = Val.ToString();
                    }
                    else
                    {
                        propertyValue = Convert.ChangeType(Val, pi.PropertyType);
                    }
                }
                else
                {
                    propertyValue = Val;
                }
            }
            // 将属性值存入字典中.
            if (Data.ContainsKey(propertyName))
            {
                Data[propertyName] = propertyValue;
            }
            else
            {
                Data.Add(propertyName, propertyValue);
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取实体中指定属性的值.
        /// </summary>
        /// <param name="propertyName">属性名称.</param>
        /// <typeparam name="T">属性值的数据类型.</typeparam>
        /// <returns>返回指定属性的值.</returns>
        public T GetValue <T>(string propertyName)
        {
            // 首先获得属性成员信息,以便进行属性值的数据有效性验证.
            Type         typeMe = this.GetType();
            PropertyInfo pi     = typeMe.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public);

            if (pi == null)
            {
                throw (new Exception(string.Format("未找到实体的属性:{0}", propertyName)));
            }
            // 获得得用于验证属性值的有效性的特性信息.
            EntityPropertyAttribute attribute = pi.GetCustomAttribute(typeof(EntityPropertyAttribute)) as EntityPropertyAttribute;

            if (attribute == null) // 若属性未指定相关的特征,则创建默认的数据有效性特征.
            {
                attribute = new EntityPropertyAttribute();
            }
            return(GetValue <T>(propertyName, attribute.DefaultValue));
        }