给一个Entity的字段付初始化值(C#)

给一个类去分别赋值,是一个很繁琐切无趣的工作。
那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式。
 
具体的想法就是,利用类的属性,取出所有的字段,然后,根据字段的类型来初始化不同的字段。
/// <summary>
/// エンティティのnull項目が初期化する
///     数字の項目:ゼロ
///     文字列の項目:空白
///     ★値があるの項目はそのままにする
/// </summary>
/// <typeparam name="T">タイプ</typeparam>
/// <param name="entity">エンティティ</param>
/// <returns>初期化結果</returns>
public static T InitEntityValue<T>(T entity)
{
    if (entity == null)
    {
        // ヌルの場合、対象インスタンスを作成する
        entity = (T)Activator.CreateInstance(typeof(T));
    }

    var type = entity.GetType();

    var items = type.GetProperties();
    foreach (var item in items)
    {
        if (item.GetValue(entity, null) != null)
        {
            continue;
        }

        if (typeof(string).Equals(item.PropertyType))
        {
            item.SetValue(entity, string.Empty, null);
        }
        else if (typeof(bool).Equals(item.PropertyType))
        {
            item.SetValue(entity, false, null);
        }
        else if (typeof(int).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0, null);
        }
        else if (typeof(long).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0L, null);
        }
        else if (typeof(float).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0F, null);
        }
        else if (typeof(double).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0D, null);
        }
        else if (typeof(decimal).Equals(item.PropertyType))
        {
            item.SetValue(entity, decimal.Zero, null);
        }
        else if (typeof(int?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0, null);
        }
        else if (typeof(long?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0L, null);
        }
        else if (typeof(float?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0F, null);
        }
        else if (typeof(double?).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0D, null);
        }
        else if (typeof(decimal?).Equals(item.PropertyType))
        {
            item.SetValue(entity, decimal.Zero, null);
        }
        else if (typeof(Nullable<int>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0, null);
        }
        else if (typeof(Nullable<long>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0L, null);
        }
        else if (typeof(Nullable<float>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0F, null);
        }
        else if (typeof(Nullable<double>).Equals(item.PropertyType))
        {
            item.SetValue(entity, 0D, null);
        }
        else if (typeof(Nullable<decimal>).Equals(item.PropertyType))
        {
            item.SetValue(entity, decimal.Zero, null);
        }
        else
        {
            item.SetValue(entity, InitEntityValue(item.GetValue(entity, null)), null);
        }
    }

    return entity;
}

 

转载于:https://www.cnblogs.com/gekal/p/3746537.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值