zoukankan
html css js c++ java
来自CS的序列化的类
using
System;
using
System.Collections.Specialized;
using
System.Globalization;
using
System.IO;
using
System.Runtime.Serialization;
using
System.Runtime.Serialization.Formatters.Binary;
using
System.Runtime.Serialization.Formatters.Soap;
using
System.Security;
using
System.Security.Permissions;
using
System.Text;
using
System.Xml;
using
System.Xml.Serialization;
namespace
Cvv.Components
{
public
class
Serializer
{
public
static
readonly
bool
CanBinarySerialize;
static
Serializer()
{
SecurityPermission permission
=
new
SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
try
{
permission.Demand();
CanBinarySerialize
=
true
;
}
catch
(SecurityException)
{
CanBinarySerialize
=
false
;
}
}
private
Serializer()
{
}
public
static
object
ConvertFileToObject(
string
path, Type objectType)
{
object
obj2
=
null
;
if
((path
!=
null
)
&&
(path.Length
>
0
))
{
using
(FileStream stream
=
new
FileStream(path, FileMode.Open, FileAccess.Read))
{
obj2
=
new
XmlSerializer(objectType).Deserialize(stream);
stream.Close();
}
}
return
obj2;
}
public
static
void
ConvertFromNameValueCollection(NameValueCollection nvc,
ref
string
keys,
ref
string
values)
{
ConvertFromNameValueCollection(nvc,
ref
keys,
ref
values,
false
);
}
public
static
void
ConvertFromNameValueCollection(NameValueCollection nvc,
ref
string
keys,
ref
string
values,
bool
allowEmptyStrings)
{
if
((nvc
!=
null
)
&&
(nvc.Count
!=
0
))
{
StringBuilder builder
=
new
StringBuilder();
StringBuilder builder2
=
new
StringBuilder();
int
num
=
0
;
foreach
(
string
text
in
nvc.AllKeys)
{
if
(text.IndexOf(
'
:
'
)
!=
-
1
)
{
throw
new
ArgumentException(
"
ExtendedAttributes Key can not contain the character \
"
:\
""
);
}
string
text2
=
nvc[text];
if
((allowEmptyStrings
&&
(text2
!=
null
))
||
!
TypeHelper.IsNullorEmpty(text2))
{
builder.AppendFormat(
"
{0}:S:{1}:{2}:
"
, text, num, text2.Length);
builder2.Append(text2);
num
+=
text2.Length;
}
}
keys
=
builder.ToString();
values
=
builder2.ToString();
}
}
public
static
object
ConvertSOAPToObject(
string
xml, Type objType)
{
MemoryStream serializationStream
=
null
;
object
obj2;
try
{
IFormatter formatter
=
new
SoapFormatter();
serializationStream
=
new
MemoryStream(Encoding.Default.GetBytes(xml));
obj2
=
formatter.Deserialize(serializationStream);
}
finally
{
if
(serializationStream
!=
null
)
{
serializationStream.Close();
}
}
return
obj2;
}
public
static
byte
[] ConvertToBytes(
object
objectToConvert)
{
byte
[] buffer
=
null
;
if
(CanBinarySerialize)
{
BinaryFormatter formatter
=
new
BinaryFormatter();
using
(MemoryStream serializationStream
=
new
MemoryStream())
{
formatter.Serialize(serializationStream, objectToConvert);
serializationStream.Position
=
0
;
buffer
=
new
byte
[serializationStream.Length];
serializationStream.Read(buffer,
0
, buffer.Length);
serializationStream.Close();
}
}
return
buffer;
}
public
static
byte
[] ConvertToBytes(
string
s)
{
return
Convert.FromBase64String(s);
}
public
static
NameValueCollection ConvertToNameValueCollection(
string
keys,
string
values)
{
NameValueCollection values2
=
new
NameValueCollection();
if
(((keys
!=
null
)
&&
(values
!=
null
))
&&
((keys.Length
>
0
)
&&
(values.Length
>
0
)))
{
char
[] separator
=
new
char
[]
{
'
:
'
}
;
string
[] textArray
=
keys.Split(separator);
for
(
int
i
=
0
; i
<
(textArray.Length
/
4
); i
++
)
{
int
startIndex
=
int
.Parse(textArray[(i
*
4
)
+
2
], CultureInfo.InvariantCulture);
int
length
=
int
.Parse(textArray[(i
*
4
)
+
3
], CultureInfo.InvariantCulture);
string
text
=
textArray[i
*
4
];
if
(((textArray[(i
*
4
)
+
1
]
==
"
S
"
)
&&
(startIndex
>=
0
))
&&
(values.Length
>=
(startIndex
+
length)))
{
values2[text]
=
values.Substring(startIndex, length);
}
}
}
return
values2;
}
public
static
object
ConvertToObject(
byte
[] byteArray)
{
object
obj2
=
null
;
if
((CanBinarySerialize
&&
(byteArray
!=
null
))
&&
(byteArray.Length
>
0
))
{
BinaryFormatter formatter
=
new
BinaryFormatter();
using
(MemoryStream serializationStream
=
new
MemoryStream())
{
serializationStream.Write(byteArray,
0
, byteArray.Length);
serializationStream.Position
=
0
;
if
(byteArray.Length
>
4
)
{
obj2
=
formatter.Deserialize(serializationStream);
}
serializationStream.Close();
}
}
return
obj2;
}
public
static
object
ConvertToObject(
string
xml, Type objectType)
{
object
obj2
=
null
;
if
(
!
TypeHelper.IsNullorEmpty(xml))
{
using
(StringReader textReader
=
new
StringReader(xml))
{
obj2
=
new
XmlSerializer(objectType).Deserialize(textReader);
textReader.Close();
}
}
return
obj2;
}
public
static
object
ConvertToObject(XmlNode node, Type objectType)
{
object
obj2
=
null
;
if
(node
!=
null
)
{
using
(StringReader textReader
=
new
StringReader(node.OuterXml))
{
obj2
=
new
XmlSerializer(objectType).Deserialize(textReader);
textReader.Close();
}
}
return
obj2;
}
public
static
string
ConvertToSOAPString(
object
obj)
{
MemoryStream serializationStream
=
null
;
string
text;
try
{
serializationStream
=
new
MemoryStream();
IFormatter formatter
=
new
SoapFormatter();
formatter.Serialize(serializationStream, obj);
int
count
=
(
int
)serializationStream.Length;
byte
[] buffer
=
new
byte
[count];
serializationStream.Seek((
long
)
0
, SeekOrigin.Begin);
serializationStream.Read(buffer,
0
, count);
UTF8Encoding encoding
=
new
UTF8Encoding();
text
=
encoding.GetString(buffer).Trim();
}
finally
{
if
(serializationStream
!=
null
)
{
serializationStream.Close();
}
}
return
text;
}
public
static
string
ConvertToString(
byte
[] arr)
{
return
Convert.ToBase64String(arr);
}
public
static
string
ConvertToString(
object
objectToConvert)
{
string
text
=
null
;
if
(objectToConvert
!=
null
)
{
XmlSerializer serializer
=
new
XmlSerializer(objectToConvert.GetType());
using
(StringWriter writer
=
new
StringWriter(CultureInfo.InvariantCulture))
{
serializer.Serialize((TextWriter)writer, objectToConvert);
text
=
writer.ToString();
writer.Close();
}
}
return
text;
}
public
static
object
LoadBinaryFile(
string
path)
{
if
(
!
File.Exists(path))
{
return
null
;
}
using
(FileStream input
=
new
FileStream(path, FileMode.Open, FileAccess.Read))
{
BinaryReader reader
=
new
BinaryReader(input);
byte
[] buffer
=
new
byte
[input.Length];
reader.Read(buffer,
0
, (
int
)input.Length);
return
ConvertToObject(buffer);
}
}
public
static
bool
SaveAsBinary(
object
objectToSave,
string
path)
{
if
((objectToSave
!=
null
)
&&
CanBinarySerialize)
{
byte
[] buffer
=
ConvertToBytes(objectToSave);
if
(buffer
!=
null
)
{
using
(FileStream output
=
new
FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
using
(BinaryWriter writer
=
new
BinaryWriter(output))
{
writer.Write(buffer);
return
true
;
}
}
}
}
return
false
;
}
public
static
void
SaveAsXML(
object
objectToConvert,
string
path)
{
if
(objectToConvert
!=
null
)
{
XmlSerializer serializer
=
new
XmlSerializer(objectToConvert.GetType());
using
(StreamWriter writer
=
new
StreamWriter(path))
{
serializer.Serialize((TextWriter)writer, objectToConvert);
writer.Close();
}
}
}
public
static
string
XMLDecode(
string
sTmp)
{
int
[] numArray
=
new
int
[]
{
0x26
,
60
,
0x3e
,
0x22
,
0x3d
,
0x27
}
;
for
(
int
i
=
0
; i
<
numArray.Length; i
++
)
{
sTmp
=
sTmp.Replace(
"
&#
"
+
numArray[i].ToString()
+
"
;
"
, ((
char
)numArray[i]).ToString());
}
return
sTmp;
}
public
static
string
XMLEncode(
string
sTmp)
{
int
[] numArray
=
new
int
[]
{
0x26
,
60
,
0x3e
,
0x22
,
0x3d
,
0x27
}
;
for
(
int
i
=
0
; i
<
numArray.Length; i
++
)
{
sTmp
=
sTmp.Replace(((
char
)numArray[i]).ToString(),
"
&#
"
+
numArray[i].ToString()
+
"
;
"
);
}
return
sTmp;
}
}
}
查看全文
相关阅读:
日积月累--小技巧之四
深入理解object C中复制对象的用法(二)
状压dp-poj-1170-Shopping Offers
linux printk函数学习
I.MX6 WIFI wireless_tools 移植
I.MX6 AW-NB177NF WIFI 驱动移植问题
VS 一些配置设置
I.MX6 boot from Micro SD
Android studio 构建太慢
Android gif 录屏
原文地址:https://www.cnblogs.com/afxcn/p/733665.html
最新文章
在Asp.Net的Global.asax中Application_Error跳转到自定义错误页无效的解决办法
jQuery触发<a>标签的点击事件后URL不跳转的解决办法
一个C#序列化时循环引用的问题
微软Sql server analysis service数据挖掘技术
实现JNI的另一种方法:使用RegisterNatives方法传递和使用Java自定义类 (转)
c++经典排序算法全集(转)
ffmpeg2.2在ubuntu下使用NDK编译——并在android工程下测试使用
android处理Back键Home键和Menu键事件(转)
Linux Makefile 教程(转)
Xcode 中设置部分文件ARC支持
热门文章
auto_ptr 浅析(转)
有单例模式 Singleton 涉及的一些防止类被继承的东西
lua的注释
IOS调试技巧:当程序崩溃的时候怎么办 xcode调试
[置顶] VB 中chr(10)、chr(13)和vblf、vbcr、vbcrlf的分别
HDU 3660 Alice and Bob's Trip
weblogic Connection has already been closed解决方法
几种排序算法性能比较
Utf-8 转 GBK
linux是一种修行
Copyright © 2011-2022 走看看