• datacontract helper


        public static class DataContractHelper
        {
    
            public static void ToDCFile<T>(this T obj, string path)  
            {
                //路径
                FileStream fs = new FileStream(path, FileMode.Create);
                try
                {
                    DataContractSerializer s = new DataContractSerializer(typeof(T));
                    s.WriteObject(fs, obj);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    fs.Close();
                }
            }
    
            public static T FromDCFile<T>(string path)   
            {
                FileStream fs = new FileStream(path, FileMode.Open);
                try
                {
                    DataContractSerializer s = new DataContractSerializer(typeof(T));
                    var obj = s.ReadObject(fs);
                    return (T)obj;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return default(T);
                }
                finally
                {
                    fs.Close();
                }
    
            }
    
            public static string ToDCJsone<T>(this T obj)  where T:new()
            {
                string result = string.Empty;
                //路径
                MemoryStream ms = new MemoryStream();
                try
                {
                    DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
                    s.WriteObject(ms, obj);
                    ms.Position = 0;
                    result = (new StreamReader(ms, Encoding.UTF8)).ReadToEnd();
                    return result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return "";
                }
                finally
                {
                    ms.Close();
                }
            }
    
            public static T FromDCJsone<T>(string jsonStr)   
            {
                MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonStr));
                try
                {
                    DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
                    ms.Position = 0;
                    var obj = s.ReadObject(ms);
                    return (T)obj;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return default(T);
                }
                finally
                {
                    ms.Close();
                }
    
            }
    
        }
  • 相关阅读:
    hdu 3790 最短路径问题
    hdu 2112 HDU Today
    最短路问题 以hdu1874为例
    hdu 1690 Bus System Floyd
    hdu 2066 一个人的旅行
    hdu 2680 Choose the best route
    hdu 1596 find the safest road
    hdu 1869 六度分离
    hdu 3339 In Action
    序列化和反序列化
  • 原文地址:https://www.cnblogs.com/nocanstillbb/p/10557464.html
Copyright © 2020-2023  润新知