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 ...
随机推荐
- html5-5 HTML5表单元素和内嵌框架
html5-5 HTML5表单元素和内嵌框架 一.总结 一句话总结:单选框和多选框选的时候外面加label就可以实现选后面文字也可以选中了 1.html5如何实现文件上传? 必须加上enctype ...
- 【poj 1704】Georgia and Bob
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9776 Accepted: 3222 Description Georgia a ...
- java 线程排查问题流程
1. 通过top命令查看当前系统CPU使用情况,定位CPU使用率超过100%的进程ID:2. 通过ps aux | grep PID命令进一步确定具体的线程信息:3. 通过ps -mp pid -o ...
- [Typescript 2] Nullable Types - Avoiding null and undefined Bugs
For example you have a TS app: enum PaylerPosition { Guard, Forward, Center } interface Player { nam ...
- 循环不变式(loop invariant)
循环不变式是一种条件式(必须满足的条件,对循环而言是保持不变的,无论循环执行了多少次),循环语句没执行一次,就要求中间的结果必须符合不变式的要求. (1)进入循环语句时,不变式必须成立: (2)循环语 ...
- Curator简介
Curator简介 Curator是一个博物馆或者其它集合的监管者或者管理者,Curator有以下几个组件组成: Recipes: 实现了通用ZooKeeper的recipes, 该组件是在Frame ...
- unityd 公布android apk相关
http://game.ceeger.com/forum/read.php?tid=5918&ds=1 相关的文章非常多,我仅仅记录自己遇到的一些关键点. 1.jdk android SDK ...
- 【cordova】cordova安装步骤(windows)
原文:[cordova]cordova安装步骤(windows) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/snow_finland/artic ...
- java序列化框架(protobuf、thrift、kryo、fst、fastjson、Jackson、gson、hessian)性能对比
我们为什么要序列化 举个栗子:下雨天我们要打伞,但是之后我们要把伞折叠起来,方便我们存放.那么运用到我们java中道理是一样的,我们要将数据分解成字节流,以便存储在文件中或在网络上传输,这叫序列 ...
- Android 获取imei号码,获取手机型号和系统版本号
在AndroidManifest.xml文件中要添加 <uses-permission android:name="android.permission.READ_PHONE_STAT ...