给一个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 Pascal Script (转)
http://www.cnblogs.com/MaxWoods/p/3304954.html 摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演 ...
- 读loadBalance技术的一些笔记
以前知道loadbalance的原理,但是仅仅是浅浅的了解过,今天看了一篇 10多年前 一位大神级别人物 写的文章 顿时学习了 http://www.linuxvirtualserver.org/zh ...
- shared_ptr注意事项
对shared_ptr的Copy构造和Copy赋值,会改变引用计数,但是对shared_ptr中原始资源的Copy构造和Copy赋值,不会改变引用计数.因此存在下面的危险情况: 1.获取资源时,初始化 ...
- [转]O(n)回文子串算法 Manacher算法
这里,我介绍一下O(n)回文串处理的一种方法.Manacher算法.原文地址:http://zhuhongcheng.wordpress.com/2009/08/02/a-simple-linear- ...
- Android程序开发0基础教程(一)
程序猿学英语就上视觉英语网 Android程序开发0基础教程(一) 平台简单介绍 令人激动的Google手机操作系统平台-Android在2007年11月13日正式公布了,这是一个开放源码的操 ...
- GifCam
转载:http://blog.bahraniapps.com/?page_id=21 下载 http://pan.baidu.com/s/1c0vdHIw GifCam Note:GifCam c ...
- 如何简单便捷的搭建一个网站 - 基于Django
一.所需工具以及相关环境 1. 系统:win7,win8.1,win10(亲测可用 - 本文为win7,64位) 2. 本文使用的版本是: 1)python-2.7.11[百度云盘分享:http:// ...
- scala基础备忘
声明一个变量 声明一个常量 显式指定类型 定义一个main函数 package org.admln.scala class HelloScala { } object HelloScala { def ...
- IOS开发 xcode报错之has been modified since the precompiled header was built
转载的文章 很实用 IOS开发xcode报错之has been modified since the precompiled header was built 今天做百度地图的时候第一次发现下面错误 ...
- tcpdump常用命令
1. 只抓syn包 tcpdump -i eth1 'tcp[tcpflags] = tcp-syn' 2. 只抓ack包 tcpdump -nni xgbe1 dst host 191.168.10 ...