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

那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式。
 
具体的想法就是,利用类的属性,取出所有的字段,然后,根据字段的类型来初始化不同的字段。
  1. /// <summary>
  2. /// エンティティのnull項目が初期化する
  3. /// 数字の項目:ゼロ
  4. /// 文字列の項目:空白
  5. /// ★値があるの項目はそのままにする
  6. /// </summary>
  7. /// <typeparam name="T">タイプ</typeparam>
  8. /// <param name="entity">エンティティ</param>
  9. /// <returns>初期化結果</returns>
  10. public static T InitEntityValue<T>(T entity)
  11. {
  12. if (entity == null)
  13. {
  14. // ヌルの場合、対象インスタンスを作成する
  15. entity = (T)Activator.CreateInstance(typeof(T));
  16. }
  17.  
  18. var type = entity.GetType();
  19.  
  20. var items = type.GetProperties();
  21. foreach (var item in items)
  22. {
  23. if (item.GetValue(entity, null) != null)
  24. {
  25. continue;
  26. }
  27.  
  28. if (typeof(string).Equals(item.PropertyType))
  29. {
  30. item.SetValue(entity, string.Empty, null);
  31. }
  32. else if (typeof(bool).Equals(item.PropertyType))
  33. {
  34. item.SetValue(entity, false, null);
  35. }
  36. else if (typeof(int).Equals(item.PropertyType))
  37. {
  38. item.SetValue(entity, , null);
  39. }
  40. else if (typeof(long).Equals(item.PropertyType))
  41. {
  42. item.SetValue(entity, 0L, null);
  43. }
  44. else if (typeof(float).Equals(item.PropertyType))
  45. {
  46. item.SetValue(entity, 0F, null);
  47. }
  48. else if (typeof(double).Equals(item.PropertyType))
  49. {
  50. item.SetValue(entity, 0D, null);
  51. }
  52. else if (typeof(decimal).Equals(item.PropertyType))
  53. {
  54. item.SetValue(entity, decimal.Zero, null);
  55. }
  56. else if (typeof(int?).Equals(item.PropertyType))
  57. {
  58. item.SetValue(entity, , null);
  59. }
  60. else if (typeof(long?).Equals(item.PropertyType))
  61. {
  62. item.SetValue(entity, 0L, null);
  63. }
  64. else if (typeof(float?).Equals(item.PropertyType))
  65. {
  66. item.SetValue(entity, 0F, null);
  67. }
  68. else if (typeof(double?).Equals(item.PropertyType))
  69. {
  70. item.SetValue(entity, 0D, null);
  71. }
  72. else if (typeof(decimal?).Equals(item.PropertyType))
  73. {
  74. item.SetValue(entity, decimal.Zero, null);
  75. }
  76. else if (typeof(Nullable<int>).Equals(item.PropertyType))
  77. {
  78. item.SetValue(entity, , null);
  79. }
  80. else if (typeof(Nullable<long>).Equals(item.PropertyType))
  81. {
  82. item.SetValue(entity, 0L, null);
  83. }
  84. else if (typeof(Nullable<float>).Equals(item.PropertyType))
  85. {
  86. item.SetValue(entity, 0F, null);
  87. }
  88. else if (typeof(Nullable<double>).Equals(item.PropertyType))
  89. {
  90. item.SetValue(entity, 0D, null);
  91. }
  92. else if (typeof(Nullable<decimal>).Equals(item.PropertyType))
  93. {
  94. item.SetValue(entity, decimal.Zero, null);
  95. }
  96. else
  97. {
  98. item.SetValue(entity, InitEntityValue(item.GetValue(entity, null)), null);
  99. }
  100. }
  101.  
  102. return entity;
  103. }

给一个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. NFS错误Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno=Connection refused

    NFS报错一例 [root@bjs0- ~]# /etc/init.d/portreserve start Starting portreserve:                          ...

  2. CentOS 6.4安装本地yum源,并安装X Window System

    1.为DVD创建一个挂载目录 [root@localhost ~]# mkdir /media/CentOS 2.在Linux下挂载CentOS DVD(虚拟机挂载DVD不说了,应该都会) [root ...

  3. SMARTFORM报表程序设计(1)

    SMARTFORM是SAP提供的一款商务单据及报表设置工具,可以在FORM中实现数据的计算及转换等功能,并能在FORM创建的同时生成功能模块,为FORM和ABAP程序提供更为强大的参数接口.输入T-C ...

  4. ExtJS grid tableGrid study

    Q:  How to color the text in the grid Try: http://dev.sencha.com/playpen/docs/output/Ext.grid.TableG ...

  5. 【JSP】JSTL使用core标签总结(不断更新中)

    使用core标签 在页面中使用taglib指令指定标签URI和prefix.如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core&q ...

  6. Python基础教程之第2章 列表和元组

    D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Typ ...

  7. PHP函数spl_autoload_register()用法和__autoload()介绍(转)

    详细出处参考:http://www.jb51.net/article/29624.htm 又是框架冲突导致__autoload()失效,用spl_autoload_register()重构一下,问题解 ...

  8. Trace、Debug和TraceSource的使用以及日志设计 .

    [-] Trace 和 Debug区别 什么是Listeners 跟踪开关 使用BooleanSwitch开关 使用TraceSwitch开关 使用TraceSource代替Trace和Debug 设 ...

  9. C语言中的各种修饰符

    C允许同时使用多于一个的修饰符,这就使得可以创建一下各种类型: int board[8][8];//int数组的数组 int **ptr;//指向int的指针的指针 int *risk[10];//具 ...

  10. Google搜索技巧-从入门到精通(从此学习进步、工作顺心)

    转载:http://www.blogbus.com/koudaizhi-logs/55687286.html 一  GOOGLE简介 Google (www.google.com)是一个搜寻引擎,由某 ...