ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

一款好用的类型转换库

2021-05-29 17:30:10  阅读:171  来源: 互联网

标签:类型转换 Console string 一款 DateTime WriteLine date dr 好用


​ 这是一款方便日常研发的扩展类库,通过简单的扩展让代码可读性更高更简洁,减少繁琐的类型转换,非空判断,异常处理流程。frame work和.net core都兼容哦。

​ 目前库中包含部分基元类型的转换,基本满足日常开发。如果大家感兴趣或者有需要,欢迎提出宝贵的意见,或一起加入完善它,为我们的.Net生态奉献一点微薄之力。

  • 代码示例

  1. 基础类型

    1. bool类型:

          int intResult = true.ToInt32();
          short shortResult = false.ToInt16();
          ushort ushortResult = true.ToUInt16(); 
      
    2. int类型:

         bool convertBoolean = 123.ToBoolean();
         ushort convertUshort = 123.ToUshort();
      
    3. string类型:

          //return 123
          int strToInt = "123".ToInt32();
          //如果待转换内容包含无效字符,则返回默认值0,示例中指定默认值2,所以返回2
          int strToIntDefault = "123ABC".ToInt32(2);
      
          //return 223
          ushort strToUshort = "223".ToUShort();
          //如果待转换内容包含无效字符,则返回默认值0,示例中指定默认值3,所以返回3
          ushort strToUshortDefault = "-1234".ToUShort(3);
      
          //return true
          bool strToBoolean = "223".ToBoolean();
          //根据字符串"true"或"false"(不区分大小写)转换成bool类型,字符串"0"返回false,其它返回true。
          bool strToBooleanDefault = "sdfv".ToBoolean();
      
    4. DateTime:

              DateTime date = DateTime.Now;
              //返回"2021-05-29 00:00:00"
              string strStartDate = date.FormatBeginDate();
              //返回"2021-05-29 23:59:59"
              string strEndDate = date.FormatEndDate();
              //返回 2021-05-29 00:00:00
              DateTime startDate = date.ToBeginDate();
              //返回 2021-05-29 23:59:59
              DateTime endDate = date.ToEndDate();
              //参数0=DateTime.Now 1=DateTime.MinValue  2=DateTime.MaxValue,此示例将返回DateTime.MaxValue
              DateTime convertDate = "123ABC".ToDateTime(2);
      
  2. 集合类:

    取List中类型的某个字段组成一个新的List(虽然不知道这么改有啥意义,可能是因为我乐意)

    class EntityModel
    {
       public string ID { get; set; }
       public string Name { get; set; }
       public int Count { get; set; }
    }
    //测试数据
    List<EntityModel> lst = new List<EntityModel>();
    for (int i = 0; i < 5; i++)
    {
        lst.Add(new EntityModel() { ID = i.ToString(), Name = $"name{i}", Count = i });
    }
    //将lst中EntityModel的Count取出作为一个新的集合,等同于list.Select(l => l.Count).ToList();
    List<int> filter = lst.ToList(l => l.Count);
    
  3. ADO:

    DataTable和DataRow的一些扩展,结果大家自己测试吧,这里就不码出来了

            //生成测试数据
            DataTable dt = new DataTable();
            dt.Columns.Add("idx", typeof(int));
            dt.Columns.Add("bolean", typeof(bool));
            dt.Columns.Add("date", typeof(DateTime));
            dt.Columns.Add("string", typeof(string));

            DataRow dr = dt.NewRow();
            dr["idx"] = 0;
            dr["bolean"] = true;
            dr["date"] = DateTime.Now;
            dr["string"] = "asdasd";
            //下面是示例
            Console.WriteLine("--------DataRow extension-----------");
            Console.WriteLine("数据源:");
            foreach (DataColumn item in dt.Columns)
            {
                Console.WriteLine($"ColumnName :{item.ColumnName}\tDataType:{item.DataType.Name}\tValue:{dr[item]}");
            }
            Console.WriteLine("----------------------------------------------------------");
            //dr.ToBoolean("bolean")
            Console.WriteLine($"DataRow(bool) to boolean:\t{dr.ToBoolean("bolean")}");
            //dr.ToBoolean("idx")
            Console.WriteLine($"DataRow(int) to boolean:\t{dr.ToBoolean("idx")}");
            //dr.ToBoolean("string")
            Console.WriteLine($"DataRow(string) to boolean:\t{dr.ToBoolean("string")}");
            //dr.ToDateTimeString("date")
            Console.WriteLine($"DataRow(DateTime) to string:\t{dr.ToDateTimeString("date")}");
            //dr.ToDateTime("date")
            Console.WriteLine($"DataRow(DateTime) to DateTime:\t{dr.ToDateTime("date")}");
            Console.WriteLine();
            //测试数据
            List<EntityModel> lst = new List<EntityModel>();
            for (int i = 0; i < 5; i++)
            {
                lst.Add(new EntityModel() { ID = i.ToString(), Name = $"name{i}", Count = i });
            }
            //List转DataTable 
            DataTable dataTable = lst.ToDataTable();
            //DataTable转List
            List<EntityModel> testList = dataTable.ToList<EntityModel>();

搜索NuGet:EasyExtension

Git地址:https://github.com/cnwhm/Easy-extension

Gitee地址:https://gitee.com/cnwhm/Easy-extension

欢迎大家提出宝贵意见:cnwhm@qq.com

标签:类型转换,Console,string,一款,DateTime,WriteLine,date,dr,好用
来源: https://blog.csdn.net/cnwhm2012/article/details/117388204

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有