C#分类辅助类:属性辅助(PropertyHandler)

30 篇文章 1 订阅

名称

方法

得到类的所有属性

GetPropertyInfoArray

数据行转类型

DataRowToModel<T>

将对象转换为数据行

ModelToDataRow<T>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Wsfly
{
    /// <summary>
    /// PropertyHandler
    /// </summary>
    public class PropertyHandler
    {
        /// <summary>
        /// 得到类的所有属性
        /// </summary>
        /// <returns></returns>
        public static PropertyInfo[] GetPropertyInfoArray(Type type)
        {
            PropertyInfo[] props = null;

            try
            {
                object obj = Activator.CreateInstance(type);
                props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            }
            catch { }

            return props;
        }
        /// <summary>
        /// 数据行转类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row"></param>
        /// <returns></returns>
        public static T DataRowToModel<T>(System.Data.DataRow row)
        {
            //初始
            T obj = default(T);

            //实例化
            obj = System.Activator.CreateInstance<T>();

            //是否有数据
            if (row == null) return obj;

            //得到所有属性
            PropertyInfo[] pis = GetPropertyInfoArray(typeof(T));
            if (pis == null) return obj;

            foreach (PropertyInfo pi in pis)
            {
                try
                {
                    //是否行包含属性名列
                    if (!row.Table.Columns.Contains(pi.Name)) continue;
                    //设置值
                    pi.SetValue(obj, row[pi.Name], null);
                }
                catch { }
            }

            return obj;
        }
        /// <summary>
        /// 将对象转换为数据行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static System.Data.DataRow ModelToDataRow<T>(T obj)
        {
            //是否有对象
            if (obj == null) return null;

            //得到所有属性
            PropertyInfo[] pis = GetPropertyInfoArray(typeof(T));
            if (pis == null) return null;

            //数据表
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.NewRow();

            foreach (PropertyInfo pi in pis)
            {
                try
                {
                    System.Data.DataColumn col = new System.Data.DataColumn();
                    col.ColumnName = pi.Name;
                    col.DataType = pi.PropertyType;

                    dt.Columns.Add(col);

                    dt.Rows[0][pi.Name] = pi.GetValue(obj, null);
                }
                catch { }
            }

            return dt.Rows[0];
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MOZ-Soft

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值