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;
}
}
}
查看全文
相关阅读:
Jquery使用live导致执行的内容会重复执行
网上找的些tomact配置
DatePicker 注意点 1.不用v-model 用:value 2.配合on-change进行回调 3.初始值 当天的用 (new Date()).toLocaleDateString().replace(///g, '-')
Springboot框架 idea 工具 解决代码修改需要重新启动的方式
合同签订
ORACLE链接错误解决方案
oracle中的split
axis2和springboot冲突问题
ActiveMQ中文乱码问题
AXIS2的接口调用常见以及不常见问题
原文地址:https://www.cnblogs.com/afxcn/p/733665.html
最新文章
挂载
Debian/Ubuntu清理硬盘空间的8个技巧(转)
oracle 恢复被删表
SqlSession was not registered for synchronization because synchronization is not active
新下载的spring boot项目 启动报数据库连接错误
折叠代码块 C#用 #region和#endregion java用 //region和//endregion
Gitlab使用备份文件进行恢复
gitlab 安装 历史版本
CentOS7 网络配置
CentOS7 网络服务启动失败
热门文章
Java sendRedirect 外部URL跳转
RestTemplate初步使用
mysql主从数据库配置(window)
iframe.contentWindow.document.write导致的内存一直增加,解决方案
网页版vscode插件(Monaco Editor),非常好用的代码编辑器插件
mysql数据删除表与表字段的备注信息
聊一聊集群、分布式,面向服务(SOA)、微服务
编写简单的批处理文件(Mysql的安装与卸载)
mysql8.0 安装之后 提示1251错误的错误处理
iframe自适应高度
Copyright © 2011-2022 走看看