c# 将object尝试转为指定对象
主方法:
- /// <summary>
- /// 将object尝试转为指定对象
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static T ConvertObjToModel<T>(object data)
- where T : new()
- {
- if (data == null) return new T();
- // 定义集合
- T result = new T();
- // 获得此模型的类型
- Type type = typeof(T);
- string tempName = "";
- // 获得此模型的公共属性
- PropertyInfo[] propertys = result.GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- tempName = pi.Name; // 检查object是否包含此列
- // 判断此属性是否有Setter
- if (!pi.CanWrite) continue;
- try
- {
- object value = GetPropertyValue(data, tempName);
- if (value != DBNull.Value)
- {
- Type tempType = pi.PropertyType;
- pi.SetValue(result, DealHelper.GetDataByType(value, tempType), null);
- }
- }
- catch
- { }
- }
- return result;
- }
调用方法:
- /// <summary>
- /// 获取一个类指定的属性值
- /// </summary>
- /// <param name="info">object对象</param>
- /// <param name="field">属性名称</param>
- /// <returns></returns>
- public static object GetPropertyValue(object info, string field)
- {
- if (info == null) return null;
- Type t = info.GetType();
- IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
- return property.First().GetValue(info, null);
- }
- /// <summary>
- /// 将数据转为制定类型
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="data1"></param>
- /// <returns></returns>
- public static object GetDataByType(object data1, Type itype, params object[] myparams)
- {
- object result = new object();
- try
- {
- if (itype == typeof(decimal))
- {
- result = Convert.ToDecimal(data1);
- if (myparams.Length > )
- {
- result = Convert.ToDecimal(Math.Round(Convert.ToDecimal(data1), Convert.ToInt32(myparams[])));
- }
- }
- else if (itype == typeof(double))
- {
- if (myparams.Length > )
- {
- result = Convert.ToDouble(Math.Round(Convert.ToDouble(data1), Convert.ToInt32(myparams[])));
- }
- else
- {
- result = double.Parse(Convert.ToDecimal(data1).ToString("0.00"));
- }
- }
- else if (itype == typeof(Int32))
- {
- result = Convert.ToInt32(data1);
- }
- else if (itype == typeof(DateTime))
- {
- result = Convert.ToDateTime(data1);
- }
- else if (itype == typeof(Guid))
- {
- result = new Guid(data1.ToString());
- }
- else if (itype == typeof(string))
- {
- result = data1.ToString();
- }
- }
- catch
- {
- if (itype == typeof(decimal))
- {
- result = ;
- }
- else if (itype == typeof(double))
- {
- result = ;
- }
- else if (itype == typeof(Int32))
- {
- result = ;
- }
- else if (itype == typeof(DateTime))
- {
- result = null;
- }
- else if (itype == typeof(Guid))
- {
- result = Guid.Empty;
- }
- else if (itype == typeof(string))
- {
- result = "";
- }
- }
- return result;
- }
c# 将object尝试转为指定对象的更多相关文章
- 【js操作url参数】获取指定url参数值、取指定url参数并转为json对象
获取指定url参数值 /* 获取某url中的某参数值 调用:GetUrlQueryString("[url地址]","[参数名]"); */ function ...
- Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处。用来指定播放器 1 2. <object> 标签用于包含对象,比如图像、音
Atitit.web 视频播放器classid clsid 大总结quicktime,vlc 1. Classid的用处.用来指定播放器 1 2. <object> 标签用于包含对象,比如 ...
- JAVA中如何将一个json形式的字符串转为json对象或对象列表
import java.util.*; import java.text.SimpleDateFormat; import org.json.JSONObject; import org.json.J ...
- js valueOf()函数用于返回指定对象的原始值
valueOf()函数用于返回指定对象的原始值. 该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法. 对象 返回 ...
- java利用反射将pojo转为json对象
最近做以太坊钱包项目需要与前台进行json交互,写一个工具类,经普通javaBean转为json对象 package util; import java.lang.reflect.Field; imp ...
- json字符串转为json对象-jQuery.parseJSON()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- DataTable转List,DataTable转为Model对象帮助类
DataTable转List,DataTable转为Model对象帮助类 public class ModelConvertHelper<T> where T : new() { publ ...
- hive 将hive表数据查询出来转为json对象和json数组输出
一.将hive表数据查询出来转为json对象输出 1.将查询出来的数据转为一行一行,并指定分割符的数据 2.使用UDF函数,将每一行数据作为string传入UDF函数中转换为json再返回 1.准备数 ...
- 无法解析指定对象的 TargetProperty (UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)“的异常解决
最近在写动画的时候做一个倒计时的效果,就是数字从大到小的一个动画,但是当我设置要new PropertyPath("XXXXXXX")的时候却报了标题的异常,各种报错.百度了好久也 ...
随机推荐
- SQL Server 2016 在Windows Server 2012 R2 上的初步安装与远程连接实战(一):初步配置
1.安装过程没有什么说头 2.下载并安装SSMS(SQLServer Management Studio),目前已更新到2017.1 3.使用SSMS登陆数据库 第一次选择Windows 身份验证,可 ...
- 新年Flag,零基础程序媛编程学习计划(持续更新ing)~~
新的一年立下了转行做程序媛的Flag,我是文科妹子,专业是做市场传销…哦不,是市场营销,算是零基础转行,目标是半年内完成自学进入公司工作,目前打算从事的方向短期目标以入行为主,以前端(可以发挥自身审美 ...
- 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 ...
- C#串口数据收发数据
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- Pycharm--flake8的配置使用
前言:Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具.Flake8检查规则灵活,支持集成额外插件,扩展性强. 一.安装flake8 进入虚拟环境: pip inst ...
- Jmeter固定定时器(Constant Timer)
如上图,Constant Timer是jmeter固定定时器元件 一般用来设置延时的,放在某个请求下,表示Constant Timer配置的指定时间后,再开始发起这个请求操作(单位:毫秒) 根据Con ...
- Maven插件maven-shade-plugin打包配置
转载以下两篇文章 https://www.cnblogs.com/fuxinci/p/3356087.html https://chenzhou123520.iteye.com/blog/197132 ...
- nodejs设置跨域访问
//设置跨域访问app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", ...
- WEBLOGIC because another WebLogic Server instance is already using this directory
错误提示:because another WebLogic Server instance is already using this directory 原因:ldap目录数据库文件被锁定,可能是w ...
- Java框架spring Boot学习笔记(二):Hello Spring Boot、以及项目属性配置
新建一个新建一个SpringBootTest工程 新建一个HelloController.java文件 package com.example.demo; import org.springframe ...