给一个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 ...
随机推荐
- 【转】深入浅出异步I/O模型
转自:http://pengpeng.iteye.com/blog/868643 从上篇文章的介绍我们知道linux内核根据TCP/IP网络模型,给我们隐藏了传输层以下的网络传输细节,我们的网络应用程 ...
- 由linux内核某个片段(container_of)引发的对于C语言的深入理解
/usr/src/linux-source-3.8.0/drivers/gpu/drm/radeon 这个文件夹以下 去找到这个文件 mkregtable.c 打开,就能够看到了. #define ...
- [Angular 2] Angular 2 Smart Components vs Presentation Components
Both Smart Components and Presentation Components receive data from Services in entirely different w ...
- error C2440:“类型转换":无法从“void (__thiscall Play1::* )(int *)”转换为“cocos2d::SEL_CallFucN
转自:http://zhidao.baidu.com/link?url=VfSSkA0xfTVwNKaKh4tqW_sXlcsK-Rb16nEtQw5zaq5_306lnwJN3Kdb-rFp-r4L ...
- PS拾色器(前景色背景色)快捷键
快捷键 I 是拾色器 X 是前后色切换
- 快速排序算法-C语言实现
注:本篇内容为翻译,之所以选择这篇进行翻译原因是该文章含有动画,能够更加直观地展示快速排序.同时,可以仔细看一下代码,代码中把结构化的思想给予了更加充分地表现.按照功能进行模块划分的思想得到了彻底地贯 ...
- Linux上安装使用boost入门指导
Data Mining Linux上安装使用boost入门指导 获得boost boost分布 只需要头文件的库 使用boost建立一个简单的程序 准备使用boost二进制文件库 把你的程序链接到bo ...
- git的一些命令
因为项目的原因,大家把项目托管到git上,然后我不会,队友就传了一个廖雪峰的git教程,讲的很详细,不会用git的同学,可以在http://pan.baidu.com/s/1pKizolP上下载,这是 ...
- C# 程序集反射
namespace AssemblyLibrary { public class AssemblyLibrary { public static object LoadAssembly(string ...
- C# 使用GDI+绘制漂亮的MenuStrip和ContextMenuStrip皮肤
通过上面的效果截图可以看到,重绘后的MenuStrip和ContextMenuStrip可以添加自己的LOGO信息,实现了类似OFFICE2007的菜单显示效果. .NET对菜单控件的绘制提供了一个抽 ...