给一个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