给一个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 ...
随机推荐
- Remobjects SDK 服务器搭建
for delphi: 在工程文件源码中,有一个编译字 {#ROGEN: ***.rodl},将它的名字改成 指定的 rodl 即可自动生成相关文件,一般默认为 NewService.
- IOS 7 Study - UISegmentedControl
You would like to present a few options to your users from which they can pick anoption, through a U ...
- 【JavsScript】当 JavaScript 从入门到提高前需要注意的细节:变量部分
在javaScript中变量使用var声明的变量是当前作用域的变量,不使用var声明的则肯定是全局变量. http://msdn.microsoft.com/zh-cn/library/dn64545 ...
- poj3041-Asteroids , 二分图的最小顶点覆盖数 = 最大匹配数
点击打开链接 Konig定理:二分图的最小顶点覆盖数 = 二分图的最大匹配数 题意: 在N*N的网络中有K颗小行星.小行星i的位置是(Ri, Ci).如今有一个强力的武器可以用一发光束将一整行或一整列 ...
- 剑指 offer set 4 矩形覆盖
总结 1. 斐波那契数列的变形题, 但是稍有隐晦, 有点意思 2. 求解 f(3) 时, 最后一块矩形可以竖着放, 也可以两块矩形横着放, 分别对应于 f(2) 和 f(1) ------------ ...
- vm.dirty_ratio & vm.dirty_background_ratio
https://lonesysadmin.net/2013/12/22/better-linux-disk-caching-performance-vm-dirty_ratio/ Better Lin ...
- CPU 100%
http://www.cnblogs.com/xuehong1985/articles/757060.html
- select,epoll的比较
机制: select:只支持水平触发(数据不处理完无限通知) epoll:支持水平触发和边缘触发(仅通知一次) 单进程监控FD个数 select: 由FD_SETSIZE设置,默认值是2048.在大量 ...
- windows和linux中检查端口是否被占用
一.windows 1.查询端口占用情况 cmd > netstat -ano 2.查询8080端口是否被占用 cmd > netstat -ano|findstr 8080 3.查询哪个 ...
- socketpair理解
转载:http://liulixiaoyao.blog.51cto.com/1361095/533469/ 今天跟人谈到socketpair的问题,晚上回来写了个程序验证下自己的猜测! 先说说我的理解 ...