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 = ;
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的更多相关文章
- [C#] 简单的 Helper 封装 -- RegularExpressionHelper
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- handlebars自定义helper的写法
handlebars相对来讲算一个轻量级.高性能的模板引擎,因其简单.直观.不污染HTML的特性,我个人特别喜欢.另一方面,handlebars作为一个logicless的模板,不支持特别复杂的表达式 ...
- 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": { " ...
- 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 ...
- JavaScript模板引擎artTemplate.js——template.helper()方法
上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...
- [ASP.NET MVC 小牛之路]13 - Helper Method
我们平时编程写一些辅助类的时候习惯用“XxxHelper”来命名.同样,在 MVC 中用于生成 Html 元素的辅助类是 System.Web.Mvc 命名空间下的 HtmlHelper,习惯上我们把 ...
- asp.net MVC helper 和自定义函数@functions小结
asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的切换,大大提升了我们的开发效率.但是Razor语法还是有一些棉花糖值得我们了解一下,可以更加强劲的提升我们 ...
- C# random helper class
项目中经常需要模拟些假数据,来做测试.这个随机生成数据的helper类就应用而生: using System; using System.Text; using System.Windows.Me ...
- @helper函数使用方法
这个函数方法,我也是通过别人博客看到的,感觉不错和大家一起学习分享一下. 1.自定义函数方法,只在同一个view视图文件里调用 Controller public ActionResult Index ...
随机推荐
- ChangeWindowMessageFilterEx 概述(用于取消低权限程序向高权限程序发送消息不成功的限制,分6个等级)
ChangeWindowMessageFilterEx 函数,为指定窗口修改用户界面特权隔离 (UIPI) 消息过滤器. 函数原型: BOOL WINAPI ChangeWindowMessageFi ...
- Django之模板过滤器
Django 模板过滤器也是我们在以后基于 Django 网站开发过程中会经常遇到的,如显示格式的转换.判断处理等.以下是 Django 过滤器列表,希望对为大家的开发带来一些方便. 一.形式:小写 ...
- 【9107】Hanoi双塔问题(NOIP2007)
Time Limit: 10 second Memory Limit: 2 MB 问题描述 给定A,B,C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的 ...
- 5.7-基于Binlog+Position的复制搭建
基本环境 Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.56.157 ...
- 学习web开发遇到几个细节问题
1.在jsp中使用jsp表达式在input标签中时,避免直接和结束"/"相连 2.提取input select 标签内的内容,使用...value提取其值 3.form中含有一个o ...
- tomcat 去除端口和项目名/使用域名访问/修改超链接为IP地址
1.打开tomcat/conf/server.xml 在host标签中加入 <Context docBase="项目名" path="" reloada ...
- RGB 的调色
通过 RGB 三通道所占比的组合(通过三元素长的元组存储),可呈现丰富的色彩样式: [0, 0, 0] ⇒ 黑色,[1, 1, 1] ⇒ 白色 [1, 0, 0] ⇒ 红色,[0, 1, 0] ⇒ 绿 ...
- matlab 格式化文本文件的解析
比如这样一种格式化的文本文件,文件说明及下载地址:/pub/machine-learning-databases/statlog/german/ 的索引 fid = fopen('german.dat ...
- .NET 即时通信,WebSocket
.NET 即时通信,WebSocket 即时通信常用手段 1.第三方平台 谷歌.腾讯 环信等多如牛毛,其中谷歌即时通信是免费的,但免费就是免费的并不好用.其他的一些第三方一般收费的,使用要则限流(1s ...
- 使用JScript脚本批量修改VC工程设置
作者:朱金灿 来源:http://blog.csdn.net/clever101 很多时候升级了第三方库,需要对很多工程修改设置.在VS中按住Ctrl键确实可以多选工程,但通过这样做也有麻烦的地方:一 ...