• 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+" "+PropertyValue);
    }
    T t = new T();
    Console.WriteLine("赋值给新对象的值如下: ");
    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 + " " + PropertyValue);

    }
    if (S2 == null)
    {
    Console.WriteLine(" Deserialized object is null (Nothing in VB)");
    }
    else
    {
    Console.WriteLine(" Deserialized type: {0}", S2.GetType());
    }


    }


    }
    }
    }

  • 相关阅读:
    八、运维管理链码
    六、编写第一个应用【外部nodejs调用】
    七、链码
    五、005-环境安装【docker、fabric】
    Webpack 4 SplitChunksPlugin配置方案(转)
    转:webpack代码压缩优化
    这样使用 GPU 渲染 CSS 动画(转)
    express中间件--Morgan 日志记录
    盘点 React 16.0 ~ 16.5 主要更新及其应用
    求最大容积
  • 原文地址:https://www.cnblogs.com/kexb/p/4556842.html
Copyright © 2020-2023  润新知