给一个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 ...
随机推荐
- Myeclipse如何整合tomcat
.在本机上安装MyEclipse和Tomcat 5软件程序 2.运行MyEclipse,设置与Tomcat 5服务器的连接,如下图所示: 选择Window--->Preferences,点击进入 ...
- 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 ...
- Cocos2d-x 3.0 动作
http://blog.csdn.net/lnb333666/article/details/16858635 //运行一个action动作对象 runAction("action对象&qu ...
- 目标检測的图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...
- jquery ui 改写cloes事件
htmlAjax:{//模板ajax请求参数设置项 url:"template/task/task_create.html", data ...
- PHP手册总结《预定义变量》
一:$_GET GET 是通过 urldecode() 传递的. 二:$_SERVER 这个数组中的项目由 Web 服务器创建.不能保证每个服务器都提供全部项目:服务器可能会忽略一些 三:$argc, ...
- Booting ARM Linux
来源:linux-2.6.30.4/Documentation/arm/Booting ARM Linux Booting ARM Linux ================= ...
- Data Structure 之 最优二叉树
给定n个权值作为n的叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近. ...
- 对C++/CLR的一些评价
我以后要是再用这东西,我自砍双手
- IOS 按比例裁剪图片
拍照或者从图片库中获取图片 操作过程中容易闪退,也总会有内存压力警告,第一步,首先可以考虑裁剪图片,实际上可能不需要那么大的.其次可以考虑把耗时的比如存储过程放进线程. 这里封装裁剪图片的类方法. / ...