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. [Angular] Ngrx/effects, Action trigger another action

    @Injectable() export class LoadUserThreadsEffectService { constructor(private action$: Actions, priv ...

  2. UE4的JSON读写方式<二>

    声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/43794409 Json的Writer博客地址: http:// ...

  3. 【codeforces 779A】Pupils Redistribution

    [题目链接]:http://codeforces.com/contest/779/problem/A [题意] 让你把两个组的5个人的数目都变成一样的. 支持交换操作; 问你最少需要交换几次. [题解 ...

  4. fatal error: expat.h: No such file or directory

    在CentOS7最小安装版下,编译安装apr-util时报错: fatal error: expat.h: No such file or directory 解决办法:yum install exp ...

  5. solr 7.x 查询及高亮

    查询时的api分为两种一种是万能的set,还有一种是setxxxquery @Test public void search2() throws Exception{ HttpSolrClient s ...

  6. tomcat启动时自动加载一个类 MyServletContextListener

    目的: 我们知道在tomcat启动后,需要页面请求进行驱动来执行操作接而响应.我们希望在tomcat启动的时候能够自动运行一个后台线程,以处理我们需要的一些操作.因此需要tomcat启动时就自动加载一 ...

  7. LoadFromStr的使用中出现错误“未结束的字符串常量”

    最近遇到个奇怪的问题,就是关于js参数中待换行符时,出现了错误“为结束的字符串常量”. 解决方法是:不直接将该数据以参数形式传递,而是先将其赋值在一个隐藏的文本内,需要调用的函数里只需读取该文本里的内 ...

  8. Objective-C的基础数据结构

    类的数据结构 Class(指针) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 typedef struct objc_c ...

  9. 编译freetype 的dll

    因需要给python使用freetype库,so需要一个freetype的dll 2 steps 1. 在VC中设置输出为动态链接库 2. 修改ftoption.h 在284行增加2行代码即可 /** ...

  10. vs中添加MySql实体集流程

    默认情况下只有下图: 首先需要下载mysql为vs和ef提供的驱动(可以去官网下载对应的版本) 然后打开vs开始添加实体类 首先在NuGet中安装MySql.Data和MySql.Data.Entit ...