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 [email protected] 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());
}


}


}
}
}

你可能感兴趣的:(setvalue)