主方法:

  1. /// <summary>
  2. /// 将object尝试转为指定对象
  3. /// </summary>
  4. /// <param name="data"></param>
  5. /// <returns></returns>
  6. public static T ConvertObjToModel<T>(object data)
  7. where T : new()
  8. {
  9. if (data == null) return new T();
  10. // 定义集合
  11. T result = new T();
  12.  
  13. // 获得此模型的类型
  14. Type type = typeof(T);
  15. string tempName = "";
  16.  
  17. // 获得此模型的公共属性
  18. PropertyInfo[] propertys = result.GetType().GetProperties();
  19. foreach (PropertyInfo pi in propertys)
  20. {
  21. tempName = pi.Name; // 检查object是否包含此列
  22.  
  23. // 判断此属性是否有Setter
  24. if (!pi.CanWrite) continue;
  25.  
  26. try
  27. {
  28. object value = GetPropertyValue(data, tempName);
  29. if (value != DBNull.Value)
  30. {
  31. Type tempType = pi.PropertyType;
  32. pi.SetValue(result, DealHelper.GetDataByType(value, tempType), null);
  33.  
  34. }
  35. }
  36. catch
  37. { }
  38.  
  39. }
  40.  
  41. return result;
  42. }

调用方法:

  1. /// <summary>
  2. /// 获取一个类指定的属性值
  3. /// </summary>
  4. /// <param name="info">object对象</param>
  5. /// <param name="field">属性名称</param>
  6. /// <returns></returns>
  7. public static object GetPropertyValue(object info, string field)
  8. {
  9. if (info == null) return null;
  10. Type t = info.GetType();
  11. IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
  12. return property.First().GetValue(info, null);
  13. }
  14.  
  15. /// <summary>
  16. /// 将数据转为制定类型
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. /// <param name="data1"></param>
  20. /// <returns></returns>
  21. public static object GetDataByType(object data1, Type itype, params object[] myparams)
  22. {
  23. object result = new object();
  24. try
  25. {
  26. if (itype == typeof(decimal))
  27. {
  28. result = Convert.ToDecimal(data1);
  29. if (myparams.Length > )
  30. {
  31. result = Convert.ToDecimal(Math.Round(Convert.ToDecimal(data1), Convert.ToInt32(myparams[])));
  32. }
  33. }
  34. else if (itype == typeof(double))
  35. {
  36.  
  37. if (myparams.Length > )
  38. {
  39. result = Convert.ToDouble(Math.Round(Convert.ToDouble(data1), Convert.ToInt32(myparams[])));
  40. }
  41. else
  42. {
  43. result = double.Parse(Convert.ToDecimal(data1).ToString("0.00"));
  44. }
  45. }
  46. else if (itype == typeof(Int32))
  47. {
  48. result = Convert.ToInt32(data1);
  49. }
  50. else if (itype == typeof(DateTime))
  51. {
  52. result = Convert.ToDateTime(data1);
  53. }
  54. else if (itype == typeof(Guid))
  55. {
  56. result = new Guid(data1.ToString());
  57. }
  58. else if (itype == typeof(string))
  59. {
  60. result = data1.ToString();
  61. }
  62. }
  63. catch
  64. {
  65. if (itype == typeof(decimal))
  66. {
  67. result = ;
  68. }
  69. else if (itype == typeof(double))
  70. {
  71. result = ;
  72. }
  73. else if (itype == typeof(Int32))
  74. {
  75. result = ;
  76. }
  77. else if (itype == typeof(DateTime))
  78. {
  79. result = null;
  80. }
  81. else if (itype == typeof(Guid))
  82. {
  83. result = Guid.Empty;
  84. }
  85. else if (itype == typeof(string))
  86. {
  87. result = "";
  88. }
  89. }
  90. return result;
  91. }

c# 将object尝试转为指定对象的更多相关文章

  1. 【js操作url参数】获取指定url参数值、取指定url参数并转为json对象

    获取指定url参数值 /* 获取某url中的某参数值 调用:GetUrlQueryString("[url地址]","[参数名]"); */ function ...

  2. Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处。用来指定播放器 1 2. <object> 标签用于包含对象,比如图像、音

    Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处.用来指定播放器 1 2. <object> 标签用于包含对象,比如 ...

  3. JAVA中如何将一个json形式的字符串转为json对象或对象列表

    import java.util.*; import java.text.SimpleDateFormat; import org.json.JSONObject; import org.json.J ...

  4. js valueOf()函数用于返回指定对象的原始值

    valueOf()函数用于返回指定对象的原始值. 该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法. 对象 返回 ...

  5. java利用反射将pojo转为json对象

    最近做以太坊钱包项目需要与前台进行json交互,写一个工具类,经普通javaBean转为json对象 package util; import java.lang.reflect.Field; imp ...

  6. json字符串转为json对象-jQuery.parseJSON()

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. DataTable转List,DataTable转为Model对象帮助类

    DataTable转List,DataTable转为Model对象帮助类 public class ModelConvertHelper<T> where T : new() { publ ...

  8. hive 将hive表数据查询出来转为json对象和json数组输出

    一.将hive表数据查询出来转为json对象输出 1.将查询出来的数据转为一行一行,并指定分割符的数据 2.使用UDF函数,将每一行数据作为string传入UDF函数中转换为json再返回 1.准备数 ...

  9. 无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决

    最近在写动画的时候做一个倒计时的效果,就是数字从大到小的一个动画,但是当我设置要new PropertyPath("XXXXXXX")的时候却报了标题的异常,各种报错.百度了好久也 ...

随机推荐

  1. SQL Server 2016 在Windows Server 2012 R2 上的初步安装与远程连接实战(一):初步配置

    1.安装过程没有什么说头 2.下载并安装SSMS(SQLServer Management Studio),目前已更新到2017.1 3.使用SSMS登陆数据库 第一次选择Windows 身份验证,可 ...

  2. 新年Flag,零基础程序媛编程学习计划(持续更新ing)~~

    新的一年立下了转行做程序媛的Flag,我是文科妹子,专业是做市场传销…哦不,是市场营销,算是零基础转行,目标是半年内完成自学进入公司工作,目前打算从事的方向短期目标以入行为主,以前端(可以发挥自身审美 ...

  3. vue-cli 报Module build failed: Error: No parser and no file path given, couldn't infer a parser.错的解决方法

    出错提示如下: ERROR Failed to compile with errors :: error in ./src/App.vue Module build failed: Error: No ...

  4. C#串口数据收发数据

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  5. Pycharm--flake8的配置使用

    前言:Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具.Flake8检查规则灵活,支持集成额外插件,扩展性强. 一.安装flake8 进入虚拟环境: pip inst ...

  6. Jmeter固定定时器(Constant Timer)

    如上图,Constant Timer是jmeter固定定时器元件 一般用来设置延时的,放在某个请求下,表示Constant Timer配置的指定时间后,再开始发起这个请求操作(单位:毫秒) 根据Con ...

  7. Maven插件maven-shade-plugin打包配置

    转载以下两篇文章 https://www.cnblogs.com/fuxinci/p/3356087.html https://chenzhou123520.iteye.com/blog/197132 ...

  8. nodejs设置跨域访问

    //设置跨域访问app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", ...

  9. WEBLOGIC because another WebLogic Server instance is already using this directory

    错误提示:because another WebLogic Server instance is already using this directory 原因:ldap目录数据库文件被锁定,可能是w ...

  10. Java框架spring Boot学习笔记(二):Hello Spring Boot、以及项目属性配置

    新建一个新建一个SpringBootTest工程 新建一个HelloController.java文件 package com.example.demo; import org.springframe ...