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 = ;
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 = ;
var obj = s.ReadObject(ms);
return (T)obj;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return default(T);
}
finally
{
ms.Close();
} } }

datacontract helper的更多相关文章

  1. [C#] 简单的 Helper 封装 -- RegularExpressionHelper

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. handlebars自定义helper的写法

    handlebars相对来讲算一个轻量级.高性能的模板引擎,因其简单.直观.不污染HTML的特性,我个人特别喜欢.另一方面,handlebars作为一个logicless的模板,不支持特别复杂的表达式 ...

  3. Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value '"*, Microsoft.AspNet.Mvc.TagHelpers"'

    project.json 配置: { "version": "1.0.0-*", "compilationOptions": { " ...

  4. VS2015突然报错————Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value 'Microsoft.AspNet.Mvc.Razor.TagHelpers.UrlResolutionTagHelper

    Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with ...

  5. JavaScript模板引擎artTemplate.js——template.helper()方法

    上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...

  6. [ASP.NET MVC 小牛之路]13 - Helper Method

    我们平时编程写一些辅助类的时候习惯用“XxxHelper”来命名.同样,在 MVC 中用于生成 Html 元素的辅助类是 System.Web.Mvc 命名空间下的 HtmlHelper,习惯上我们把 ...

  7. asp.net MVC helper 和自定义函数@functions小结

    asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...

  8. C# random helper class

      项目中经常需要模拟些假数据,来做测试.这个随机生成数据的helper类就应用而生: using System; using System.Text; using System.Windows.Me ...

  9. @helper函数使用方法

    这个函数方法,我也是通过别人博客看到的,感觉不错和大家一起学习分享一下. 1.自定义函数方法,只在同一个view视图文件里调用 Controller public ActionResult Index ...

随机推荐

  1. 结构体(struct)与类(class)

    在 C++ 的范畴里,除了 struct 结构体不支持访问权限修饰符(private.protected.public)外,已不再区分二者的其他方面语法上的差异. 因此,在 C++ 编程中,如果一个类 ...

  2. Android NDK对象的引用-全局引用,局部引用,弱引用

    百度了一下,google了一下,关于NDK引用的介绍无10篇中就有9.9篇是相同的,对于这种问题,我只能呜呼哀哉了!! 局部引用(函数内部对象类型变量):在C或C++中,局部变量表示只运行范围局限在该 ...

  3. android WebView总 结

    浏览器控件是每个开发环境都具备的,这为马甲神功提供了用武之地,windows的有webbrowser,android和ios都有webview.只是其引擎不同,相对于微软的webbrowser,and ...

  4. TI_DSP_SRIO - Doorbell原理

    前文介绍到SRIO有多种类型的包,当中包括了Doorbell包,Doorbell是一种高速的通知类型的短消息,包头和携带信息都非常短,用于master srio设备通知slave srio设备,可用于 ...

  5. [UWP]使用Acrylic(亚克力)

    原文:[UWP]使用Acrylic(亚克力) 1. 前言 在 如何使用Fluent Design System 这篇文章里已经简单介绍过Reveal的用法,这篇再详细介绍其它内容. 自Windows ...

  6. 三种方法解决 Failed to start LSB: Bring up/down networking 问题

    感谢朋友支持本博客.欢迎共同探讨交流.因为能力和时间有限,错误之处在所难免.欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...

  7. 【p094】道路游戏

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有n个机器人工厂,两个相邻机器人工厂之间由 ...

  8. Word 2010/2013 菜单栏添加 MathType 菜单

    一般对于 office 的高级版本,比如 word 2010/2013,在手动安装 mathtype 之后,并不会再 word 的菜单栏,创建 mathtype 按钮,此时需要按照如下步骤,手动导入 ...

  9. Azure Messaging-ServiceBus Messaging

    Azure Messaging-ServiceBus Messaging 上篇博文中我们介绍了Azure Messaging的重复消息机制.At most once 和At least once. A ...

  10. idea 搭建 SpringBoot 集成 mybatis

    编译器:IDEA2018.2.3 环境:win10,jdk1.8,maven3.4 数据库:mysql 5.7 备注:截图较大,如果看不清,可以在图片上右键=>在新标签页中打开   查看高清大图 ...