给一个类去分别赋值,是一个很繁琐切无趣的工作。

那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式。
 
具体的想法就是,利用类的属性,取出所有的字段,然后,根据字段的类型来初始化不同的字段。
/// <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, , 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, , 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, , 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;
}

给一个Entity的字段付初始化值(C#)的更多相关文章

  1. readonly 只读字段的初始化值确定|static 字段的初始值确定

    类的初始化顺序 如下: 第一次实例化Son============================ C#编译器缺省将每一个成员变量初始化为他的默认值Son静态字段Son静态构造函数Son字段Fathe ...

  2. c#为字段设置默认值,以及构造函数初始化List对象。

    1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...

  3. 利用Entity Framework修改指定字段中的值

    利用Entity Framework修改指定字段中的值一般我们编辑某些模型的时候会用到类似这样的代码: [HttpPost] public ActionResult Edit(Article mode ...

  4. SQL语句:一个表,通过一个字段查找另外一个字段不相同值

    select * from [dbo].[Sys_MemberKey] a where exists(select * from [Sys_MemberKey] b where a.FMachineC ...

  5. mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子

    mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)mysql字符串拼接cancat实战例子: mysql update set 多个字段相加,如果是数字相加可以直接用+号(注:hund ...

  6. sql修改一个字段多个值

    UPDATE 表名 SET 修改的字段=REPLACE(修改的字段,'修改的值','新值');

  7. JAVA字段的初始化规律

    JAVA字段的初始化规律 1.类的构造方法 (1)“构造方法”,也称为“构造函数”,当创建一个对象时,它的构造方法会被自动调用.构造方法与类名相同,没有返回值. (2)如果类没有定义构造函数,Java ...

  8. 实现Django ORM admin view中model字段choices取值自动更新的一种方法

    有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...

  9. Java 有关类字段的初始化

    实例代码 package text; public class MethodOverload { /** * @param args */ public static void main(String ...

随机推荐

  1. Myeclipse如何整合tomcat

    .在本机上安装MyEclipse和Tomcat 5软件程序 2.运行MyEclipse,设置与Tomcat 5服务器的连接,如下图所示: 选择Window--->Preferences,点击进入 ...

  2. IOS 7 Study - UIActivityViewController(Presenting Sharing Options)

    You want to be able to allow your users to share content inside your apps with theirfriends, through ...

  3. Cocos2d-x 3.0 动作

    http://blog.csdn.net/lnb333666/article/details/16858635 //运行一个action动作对象 runAction("action对象&qu ...

  4. 目标检測的图像特征提取之(一)HOG特征

    1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...

  5. jquery ui 改写cloes事件

    htmlAjax:{//模板ajax请求参数设置项            url:"template/task/task_create.html",            data ...

  6. PHP手册总结《预定义变量》

    一:$_GET GET 是通过 urldecode() 传递的. 二:$_SERVER 这个数组中的项目由 Web 服务器创建.不能保证每个服务器都提供全部项目:服务器可能会忽略一些 三:$argc, ...

  7. Booting ARM Linux

    来源:linux-2.6.30.4/Documentation/arm/Booting ARM Linux Booting ARM Linux            ================= ...

  8. Data Structure 之 最优二叉树

    给定n个权值作为n的叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近. ...

  9. 对C++/CLR的一些评价

    我以后要是再用这东西,我自砍双手

  10. IOS 按比例裁剪图片

    拍照或者从图片库中获取图片 操作过程中容易闪退,也总会有内存压力警告,第一步,首先可以考虑裁剪图片,实际上可能不需要那么大的.其次可以考虑把耗时的比如存储过程放进线程. 这里封装裁剪图片的类方法. / ...