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 ...
随机推荐
- poi读取excell表格
原文链接:http://blog.csdn.net/qq_37936542/article/details/79024847 最近项目需要实现一个将excell中的数据导入数据库,在网上找到这篇文章, ...
- 学习鸟哥的Linux私房菜笔记(12)——系统监视2
四.控制进程 kill :语法 kill [-signal] PID 向进程传送一个特定的讯号,默认为15(终结) kill -l :列出所有可以由kill传递的讯号 1 :重启进程 2 : ...
- Springboot系列:@SpringBootApplication注解
在使用 Springboot 框架进行开发的时候,通常我们会在 main 函数上添加 @SpringBootApplication 注解,今天为大家解析一下 @SpringBootApplicatio ...
- Android app 第三方微信支付接入详解
微信支付做了好几遍了,都没有出现什么棘手的问题,下面一一为大家分享一下,欢迎吐槽. 还是老样子,接入微信的支付要第一步添加微信支付官方的包libammsdk.jar 首先就处理略坑的一个问题,app应 ...
- springCloud你要了解的都在这(方向性)
Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...
- 如何用JS获取“ul”下边的“li”的个数
<script type="text/javascript"> function OnbtnClick() { var txtCount=document.getEle ...
- MSVC编译Boost的几种链接方式
折腾了好几个小时,终于理清了Boost链接的组合方式,记录一下. A1.动态链接Boost的动态库A2.静态链接Boost的动态库 B1.动态链接VC运行库B2.静态链接VC运行库 那么这样就有2x2 ...
- python 简单的Socket编程
python 编写server的步骤: 1第一步是创建socket对象.调用socket构造函数.如: socket = socket.socket(family, type ) family参数代表 ...
- robot framework的使用说明
robot framework安装说明1.安装python2.7.15运行安装包python-2.7.15.amd64.msi 2.robot framework(1)解压最新的压缩包如robotfr ...
- 数学思想方法-python计算战(8)-机器视觉-二值化
二值化 hreshold Applies a fixed-level threshold to each array element. C++: double threshold(InputArray ...