给一个类去分别赋值,是一个很繁琐切无趣的工作。

那我们就想办法给你一个类去初始化,或许不是一个很效率的方法,但是,从可修改的角度讲,却是一个非常不错的方式。
 
具体的想法就是,利用类的属性,取出所有的字段,然后,根据字段的类型来初始化不同的字段。
/// <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#)的更多相关文章

  1. readonly 只读字段的初始化值确定|static 字段的初始值确定

    类的初始化顺序 如下: 第一次实例化Son============================ C#编译器缺省将每一个成员变量初始化为他的默认值Son静态字段Son静态构造函数Son字段Fathe ...

  2. c#为字段设置默认值,以及构造函数初始化List对象。

    1.为字段设置默认值 /// <summary> /// 默认值 /// </summary> ; ; /// <summary> /// 页的大小 /// < ...

  3. 利用Entity Framework修改指定字段中的值

    利用Entity Framework修改指定字段中的值一般我们编辑某些模型的时候会用到类似这样的代码: [HttpPost] public ActionResult Edit(Article mode ...

  4. SQL语句:一个表,通过一个字段查找另外一个字段不相同值

    select * from [dbo].[Sys_MemberKey] a where exists(select * from [Sys_MemberKey] b where a.FMachineC ...

  5. mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)字符串拼接cancat实战例子

    mysql下的将多个字段名的值复制到另一个字段名中(批量更新数据)mysql字符串拼接cancat实战例子: mysql update set 多个字段相加,如果是数字相加可以直接用+号(注:hund ...

  6. sql修改一个字段多个值

    UPDATE 表名 SET 修改的字段=REPLACE(修改的字段,'修改的值','新值');

  7. JAVA字段的初始化规律

    JAVA字段的初始化规律 1.类的构造方法 (1)“构造方法”,也称为“构造函数”,当创建一个对象时,它的构造方法会被自动调用.构造方法与类名相同,没有返回值. (2)如果类没有定义构造函数,Java ...

  8. 实现Django ORM admin view中model字段choices取值自动更新的一种方法

    有两个表,一个是记录网站信息的site表,结构如下: CREATE TABLE `site` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `name` ) N ...

  9. Java 有关类字段的初始化

    实例代码 package text; public class MethodOverload { /** * @param args */ public static void main(String ...

随机推荐

  1. UVALive 4225 Prime Bases 贪心

    Prime Bases 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&a ...

  2. DP总结 ——QPH

    常见优化 单调队列 形式 dp[i]=min{f(k)} dp[i]=max{f(k)} 要求 f(k)是关于k的函数 k的范围和i有关 转移方法 维护一个单调递增(减)的队列,可以在两头弹出元素,一 ...

  3. WSS3.0 SDK中的不同列表类型的对应值

    Value Description 100 Generic list 101 Document library 102 Survey 103 Links list 104 Announcements ...

  4. A*算法解决八数码问题 Java语言实现

    0X00 定义 首先要明确一下什么是A*算法和八数码问题? A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法也是一种启发性的算法,也是解决许多搜索问题的有效算法.算法中的距离估 ...

  5. .NET 托管堆和垃圾回收

       托管堆基础 简述:每个程序都要使用这样或那样的资源,包括文件.内存缓冲区.屏幕空间.网络连接.....事实上,在面向对象的环境中,每个类型都代表可供程序使用的一种资源.要使用这些资源,必须为代表 ...

  6. Spring3 Security 中配置会话管理

    账户登录时,要求一个账户同时只能一人登录,配置中的步骤有三个: 1.在web.xml中配置HttpSessionEventPublisher <listener> <listener ...

  7. 【Android 界面效果38】android:inputType常用取值

    <EditText android:layout_width="fill_parent" android:layout_height="wrap_content&q ...

  8. 【Android 界面效果27】利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果

    本文主要介绍如何利用ViewPager.Fragment.PagerTabStrip实现多页面滑动效果.即google play首页.新浪微博消息(at.评论.私信.广播)页面的效果.ViewPage ...

  9. iOS 导航栏颜色字体等的自定义

    1.设置导航栏中间文字的文字颜色和文字大小 方法一:系统方法 self.title = @"下载微课";//在有navigationController的控制器中,作用与self. ...

  10. Oracle基础(六) 数据类型

    一.Oracle中的数据类型 Oracle中提供了丰富的数据类型,用来存储数据,主要包括: 1.字符类型:主要用来存储字符串类型的数据. 数据类型 长度 说明 CHAR(n BYTE/CHAR) 默认 ...