给一个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, , 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#)的更多相关文章
- readonly 只读字段的初始化值确定|static 字段的初始值确定
类的初始化顺序 如下: 第一次实例化Son============================ C#编译器缺省将每一个成员变量初始化为他的默认值Son静态字段Son静态构造函数Son字段Fathe ...
- c#为字段设置默认值,以及构造函数初始化List对象。
1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...
- 利用Entity Framework修改指定字段中的值
利用Entity Framework修改指定字段中的值一般我们编辑某些模型的时候会用到类似这样的代码: [HttpPost] public ActionResult Edit(Article mode ...
- SQL语句:一个表,通过一个字段查找另外一个字段不相同值
select * from [dbo].[Sys_MemberKey] a where exists(select * from [Sys_MemberKey] b where a.FMachineC ...
- mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子
mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)mysql字符串拼接cancat实战例子: mysql update set 多个字段相加,如果是数字相加可以直接用+号(注:hund ...
- sql修改一个字段多个值
UPDATE 表名 SET 修改的字段=REPLACE(修改的字段,'修改的值','新值');
- JAVA字段的初始化规律
JAVA字段的初始化规律 1.类的构造方法 (1)“构造方法”,也称为“构造函数”,当创建一个对象时,它的构造方法会被自动调用.构造方法与类名相同,没有返回值. (2)如果类没有定义构造函数,Java ...
- 实现Django ORM admin view中model字段choices取值自动更新的一种方法
有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...
- Java 有关类字段的初始化
实例代码 package text; public class MethodOverload { /** * @param args */ public static void main(String ...
随机推荐
- NFS错误Starting NFS quotas: Cannot register service: RPC: Unable to receive; errno=Connection refused
NFS报错一例 [root@bjs0- ~]# /etc/init.d/portreserve start Starting portreserve: ...
- CentOS 6.4安装本地yum源,并安装X Window System
1.为DVD创建一个挂载目录 [root@localhost ~]# mkdir /media/CentOS 2.在Linux下挂载CentOS DVD(虚拟机挂载DVD不说了,应该都会) [root ...
- SMARTFORM报表程序设计(1)
SMARTFORM是SAP提供的一款商务单据及报表设置工具,可以在FORM中实现数据的计算及转换等功能,并能在FORM创建的同时生成功能模块,为FORM和ABAP程序提供更为强大的参数接口.输入T-C ...
- ExtJS grid tableGrid study
Q: How to color the text in the grid Try: http://dev.sencha.com/playpen/docs/output/Ext.grid.TableG ...
- 【JSP】JSTL使用core标签总结(不断更新中)
使用core标签 在页面中使用taglib指令指定标签URI和prefix.如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core&q ...
- Python基础教程之第2章 列表和元组
D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Typ ...
- PHP函数spl_autoload_register()用法和__autoload()介绍(转)
详细出处参考:http://www.jb51.net/article/29624.htm 又是框架冲突导致__autoload()失效,用spl_autoload_register()重构一下,问题解 ...
- Trace、Debug和TraceSource的使用以及日志设计 .
[-] Trace 和 Debug区别 什么是Listeners 跟踪开关 使用BooleanSwitch开关 使用TraceSwitch开关 使用TraceSource代替Trace和Debug 设 ...
- C语言中的各种修饰符
C允许同时使用多于一个的修饰符,这就使得可以创建一下各种类型: int board[8][8];//int数组的数组 int **ptr;//指向int的指针的指针 int *risk[10];//具 ...
- Google搜索技巧-从入门到精通(从此学习进步、工作顺心)
转载:http://www.blogbus.com/koudaizhi-logs/55687286.html 一 GOOGLE简介 Google (www.google.com)是一个搜寻引擎,由某 ...