给一个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 ...
随机推荐
- os.getcwd()
1.os.getcwd():获取当前工作目录,也就是在哪个目录下运行这个程序.举例来说:假设F:\aaa\bbb\ccc目录下有个文件 TestDir.py,打印当前工作目录.进入windows命令行 ...
- SQL Server-删除表中重复的记录!
比如现在有一人员表 (表名:peosons)若想将姓名.身份证号.住址这三个字段完全相同的记录查询出来 select p1.* from persons p1,persons p ...
- URL是什么?
URL是统一资源定位符,有时也被俗称为网页地址. 如同在网络上的门牌,是因特网上标准的资源的地址(Address). 在因特网的历史上,统一资源定位符的发明是一个非常基础的步骤.统一资源定位符的语法是 ...
- cdoj 03 BiliBili, ACFun… And More! 水题
Article Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/3 Descr ...
- 防止IE缓存jquery ajax 内容
转自:http://blog.163.com/haijun_huang/blog/static/167591377201201235754763/ 解决办法: 方法一:把type改成post,并随便设 ...
- Ubuntu:Target filesystem doesn't have /sbin/init (Slax 解决)
计算机(Ubuntu)因为异常断电或是其它原因,再次启动时.非常不幸的出现: Killed mount: mounting /dev on /root/dev failed: No such file ...
- IOS之以UIBezierPath绘制饼状图
1.绘制的饼状图是通过多个扇形拼和而成,绘制一个扇形也是比较简单的,核心代码如下: 先画一条圆弧,再画半径,接着再画一条圆弧,最后闭合路径: UIBezierPath* aPath = [[UIBe ...
- JSAPI用户手册
本文档主要涵盖如何嵌入SpiderMonkey javascript引擎到你自己的c++程序中. JavaScript在浏览器端已经被广泛使用了.但是,Mozilla的javascript引擎可以被嵌 ...
- 了解javascript中的this --实例篇
对javascript this的赋值有了深一层的理解后,看一下比较复杂的情况,this的应用篇参考<对javascript this的理解>. #demo1 var name=" ...
- innodb_io_capacity >=innodb_lru_scan_depth*inoodb_buffer_pool_instances。与 checkpoint
innodb_lru_scan_depth:每个缓冲池刷脏页的能力 innodb_io_capacity: iops inoodb_buffer_pool_instances=8 :缓冲池的个数 . ...