c#解决Nullable类型的转换 (包含DataContract的序列化和反序列化以及 该例子应用在反射属性setvalue的时候有用)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ConsoleApplication11
{
[DataContract]
public class XElementContainer
{
[DataMember]
public XElement member;
public XElementContainer()
{
member = XLinqTest.CreateXElement();
}
}
[DataContract]
public class XElementNullContainer
{
[DataMember]
public XElement member;
public XElementNullContainer()
{
member = null;
}
}
/* Copyright (c) 2014 Xiamen HaiHui Software Co., Ltd. All rights reserved
*
* Create by huanglc@holworth.com at 2014-10-14 10:09:18
*
*/
///<summary>
///交易日
///</summary>
[DataContract(Namespace = "Contract.Domain")]
public class Student
{
/// <summary>
/// 交易日ID
/// </summary>
//[DataMember]
//public virtual int? BusinessDateId
//{
// get;
// set;
//}
/// <summary>
/// 最新更新日
/// </summary>
[DataMember]
public virtual DateTime? AsOfDate
{
get;
set;
}
/// <summary>
/// 交易日期
/// </summary>
[DataMember]
public virtual DateTime? TransactionDate
{
get;
set;
}
/// <summary>
/// 营业日期
/// </summary>
[DataMember]
public virtual DateTime? ProcessDate
{
get;
set;
}
/// <summary>
/// 上传日期
/// </summary>
[DataMember]
public virtual DateTime? UploadDate
{
get;
set;
}
/// <summary>
/// 计算日期
/// </summary>
[DataMember]
public virtual DateTime? ComputeDate
{
get;
set;
}
/// <summary>
/// 输入日期
/// </summary>
[DataMember]
public virtual DateTime? InputDate
{
get;
set;
}
/// <summary>
/// 结算日期
/// </summary>
[DataMember]
public virtual DateTime? SettleDate
{
get;
set;
}
/// <summary>
/// 结束日期
/// </summary>
[DataMember]
public virtual DateTime? CloseDate
{
get;
set;
}
/// <summary>
/// 发布日期
/// </summary>
[DataMember]
public virtual DateTime? PostDate
{
get;
set;
}
/// <summary>
/// 提醒日期
/// </summary>
[DataMember]
public virtual DateTime? NoticeDate
{
get;
set;
}
/// <summary>
/// 当天日期
/// </summary>
[DataMember]
public virtual DateTime? CurrentDate
{
get;
set;
}
/// <summary>
/// 前一天日期
/// </summary>
[DataMember]
public virtual DateTime? PriorDate
{
get;
set;
}
}
public class XLinqTest
{
static void Main(string[] args)
{
// Test<XElement>(CreateXElement());
// Test<XElementContainer>(new XElementContainer());
// Test<XElementNullContainer>(new XElementNullContainer());
DateTime dt = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
Test<Student>(new Student() { AsOfDate=dt, CloseDate=dt, ComputeDate=dt, CurrentDate=dt, InputDate=dt, NoticeDate=dt, PostDate=dt, PriorDate=dt, ProcessDate=dt, SettleDate=dt, TransactionDate=dt, UploadDate=dt });
Console.ReadLine();
}
public static XElement CreateXElement()
{
return new XElement(XName.Get("NameInNamespace", "http://www.adventure-works.org"));
}
public static void Test<T>(T obj) where T:new()
{
DataContractSerializer s = new DataContractSerializer(typeof(T));
using (FileStream fs = File.Open("test" + typeof(T).Name + ".xml", FileMode.Create))
{
s.WriteObject(fs,obj);
}
using (FileStream fs=File.Open("test"+typeof(T).Name+".xml",FileMode.Open))
{
Object S2=s.ReadObject(fs);
Type type = S2.GetType();
PropertyInfo[] ps = type.GetProperties();
foreach (PropertyInfo p in ps)
{
string PropertyName = p.Name;
string PropertyValue = p.GetValue(S2).ToString();
Console.WriteLine(PropertyName+"\t"+PropertyValue);
}
T t = new T();
Console.WriteLine("赋值给新对象的值如下:\r\n");
foreach (PropertyInfo p in ps)
{
string PropertyName = p.Name;
string PropertyValue = p.GetValue(S2).ToString();
if (!p.PropertyType.IsGenericType)
{
p.SetValue(t, string.IsNullOrEmpty(PropertyValue) ? null : Convert.ChangeType(PropertyValue, p.PropertyType));
}
else
{
//泛型Nullable<>
Type genericTypeDefinition = p.PropertyType.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
p.SetValue(obj, string.IsNullOrEmpty(PropertyValue) ? null : Convert.ChangeType(PropertyValue, Nullable.GetUnderlyingType(p.PropertyType)));
}
}
}
PropertyInfo[] NewPropertyInfo = type.GetProperties();
foreach (PropertyInfo p in NewPropertyInfo)
{
string PropertyName = p.Name;
string PropertyValue = p.GetValue(S2).ToString();
Console.WriteLine(PropertyName + "\t" + PropertyValue);
}
if (S2 == null)
{
Console.WriteLine(" Deserialized object is null (Nothing in VB)");
}
else
{
Console.WriteLine(" Deserialized type: {0}", S2.GetType());
}
}
}
}
}
c#解决Nullable类型的转换 (包含DataContract的序列化和反序列化以及 该例子应用在反射属性setvalue的时候有用)的更多相关文章
- C# 序列化与反序列化之DataContract与xml对子类进行序列化的解决方案
C# 序列化与反序列化之DataContract与xml对子类进行序列化的解决方案 1.DataContract继承对子类进行序列化的解决方案 第一种是在 [DataContract, KnownTy ...
- json的序列化和反序列化支持时间格式转换
.NET自带的json序列有时间格式问题,为了解决自己写了个json格式的序列化和反序列化 1.引入的命名空间 using System; using System.Collections.Gener ...
- Convert.ChangeType不能处理Nullable类型的解决办法
在做一个ORMapping功能的时候发现,Convert.ChangeType不能处理nullable类型,比如int?. 解决办法也很简单,贴出完整的代码(大部分代码来自网络),注意下面代码没经过完 ...
- Convert.ChangeType不能处理Nullable类型的解决办法(转)
https://www.cnblogs.com/patrickyu/p/3211115.html 在做一个ORMapping功能的时候发现,Convert.ChangeType不能处理nullable ...
- 彻底解决Spring mvc中时间类型的转换和序列化问题
在使用Spring mvc 进行开发时我们经常遇到前端传来的某种格式的时间字符串无法用java8时间包下的具体类型参数来直接接收.同时还有一系列的序列化 .反序列化问题,在返回前端带时间类型的同样会出 ...
- DataTable转list时 可空类型的转换问题
public class UtilHelper { public static IList<T> ConvertTo<T>(DataTable table) { if (tab ...
- spring类型自动转换——@InitBinder和Converter
spring有2种类型转换器,一种是propertyEditor,一种是Converter.虽然都是类型转换,但是还是有细微差别. 所以这里以一个例子的形式来分析一下这2种类型转换的使用场景和差别. ...
- C#高级编程笔记 2016年10月8日运算符和类型强制转换
1.checked和unchecked 运算符 C#提供了checked 和uncheckde 运算符.如果把一个代码块标记为checked, CLR就会执行溢出检查,如果发生溢出,就抛出overfl ...
- [改善Java代码]不要让类型默默转换
建议23:不要让类型默默转换 public class Client { // 光速是30万公里/秒,常量 public static final int LIGHT_SPEED = 30 * 100 ...
随机推荐
- WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)
Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...
- test20181024 ming
题意 分析 考场做法 考虑二分答案,R开到1e9就能过了. 判断答案合法,就判断时间和是否超过拥有的时间就行了.但要把di从小到大排序,不然容易验证贪心是错的. 时间复杂度\(O(n \log n)\ ...
- (经典) K&R的名著<<C程序设计语言>>二分查找
#include<stdio.h> //查找成功则返回所在下标否则返回-1 int binsearch(int A[], int n,int a) { int low, high, mid ...
- docker 数据卷 ---- 基础篇
用户在使用 Docker 的过程中,往往需要能查看容器内应用产生的数据,或者需要把容器内的数据进行备份,甚至多个容器之间进行数据的共享,这必然涉及容器的数据管理操作.容器中管理数据主要有两种方式:数据 ...
- 【转】inittab文件
原文网址:http://blog.csdn.net/shuaishuai80/article/details/6202482 一.inittab文件背景(1)init进程的作用 使用uboot下载 ...
- C# 播放器, 收藏
C#写了一个调用libvlc api实现的万能视频播放器 http://www.cnblogs.com/haibindev/archive/2011/12/21/2296173.html 引用库 ht ...
- 一个单元测试 学习 aysnc await
using System; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; name ...
- c#中的dynamic类型
dynamic是C#4.0引入的全新类型,它允许其操作略过编译期类型检查,而在运行时期处理. dynamic类型在大多数情况下和object类似,不同点在于编译器对于dynamic类型的 数据不做进一 ...
- HDFS文件系统
Hadoop 附带了一个名为 HDFS(Hadoop分布式文件系统)的分布式文件系统,专门存储超大数据文件,为整个Hadoop生态圈提供了基础的存储服务. 本章内容: 1) HDFS文件系统的特点,以 ...
- tensorflow-base_operations
# -*- coding: utf-8 -*-import tensorflow as tf# 基本的常量操作,通过构造函数返回值 定义值的操作operationsa = tf.constant(2) ...