C # resolves conversions for nullable types (including serialization and deserialization of DataContract and is useful when the example is applied to reflection properties SetValue)

Source: Internet
Author: User

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) Xiamen Haihui software Co., Ltd. All rights reserved
*
* Create by [e-mail protected] at 2014-10-14 10:09:18
*
*/


<summary>
Trading
</summary>
[DataContract (Namespace = "Contract.domain")]
public class Student
{
<summary>
Trading Day ID
</summary>
[DataMember]
public virtual int? Businessdateid
//{
Get
Set
//}

<summary>
Latest update Date
</summary>
[DataMember]
Public virtual DateTime? Asofdate
{
Get
Set
}

<summary>
Trading Date
</summary>
[DataMember]
Public virtual DateTime? TransactionDate
{
Get
Set
}

<summary>
Business Date
</summary>
[DataMember]
Public virtual DateTime? Processdate
{
Get
Set
}

<summary>
Upload Date
</summary>
[DataMember]
Public virtual DateTime? Uploaddate
{
Get
Set
}

<summary>
Calculate date
</summary>
[DataMember]
Public virtual DateTime? Computedate
{
Get
Set
}

<summary>
Enter Date
</summary>
[DataMember]
Public virtual DateTime? Inputdate
{
Get
Set
}

<summary>
Settlement Date
</summary>
[DataMember]
Public virtual DateTime? Settledate
{
Get
Set
}

<summary>
End Date
</summary>
[DataMember]
Public virtual DateTime? Closedate
{
Get
Set
}

<summary>
Release date
</summary>
[DataMember]
Public virtual DateTime? Postdate
{
Get
Set
}

<summary>
Reminder Date
</summary>
[DataMember]
Public virtual DateTime? Noticedate
{
Get
Set
}

<summary>
Date of day
</summary>
[DataMember]
Public virtual DateTime? currentdate
{
Get
Set
}

<summary>
Day before
</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 ("Value assigned to the new object is as follows: \ 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
{
Generic 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 # resolves conversions for nullable types (including serialization and deserialization of DataContract and is useful when the example is applied to reflection properties SetValue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.