来自CS的序列化的类

None.gif using  System;
None.gif
using  System.Collections.Specialized;
None.gif
using  System.Globalization;
None.gif
using  System.IO;
None.gif
using  System.Runtime.Serialization;
None.gif
using  System.Runtime.Serialization.Formatters.Binary;
None.gif
using  System.Runtime.Serialization.Formatters.Soap;
None.gif
using  System.Security;
None.gif
using  System.Security.Permissions;
None.gif
using  System.Text;
None.gif
using  System.Xml;
None.gif
using  System.Xml.Serialization;
None.gif
None.gif
namespace  Cvv.Components
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class Serializer
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static readonly bool CanBinarySerialize;
InBlock.gif
InBlock.gif        
static Serializer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            SecurityPermission permission 
= new SecurityPermission(SecurityPermissionFlag.SerializationFormatter);
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                permission.Demand();
InBlock.gif                CanBinarySerialize 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (SecurityException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                CanBinarySerialize 
= false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Serializer()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object ConvertFileToObject(string path, Type objectType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object obj2 = null;
InBlock.gif            
if ((path != null&& (path.Length > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    obj2 
= new XmlSerializer(objectType).Deserialize(stream);
InBlock.gif                    stream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return obj2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ConvertFromNameValueCollection(nvc, 
ref keys, ref values, false);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void ConvertFromNameValueCollection(NameValueCollection nvc, ref string keys, ref string values, bool allowEmptyStrings)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((nvc != null&& (nvc.Count != 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                StringBuilder builder 
= new StringBuilder();
InBlock.gif                StringBuilder builder2 
= new StringBuilder();
InBlock.gif                
int num = 0;
InBlock.gif                
foreach (string text in nvc.AllKeys)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (text.IndexOf(':'!= -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
throw new ArgumentException("ExtendedAttributes Key can not contain the character \":\"");
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
string text2 = nvc[text];
InBlock.gif                    
if ((allowEmptyStrings && (text2 != null)) || !TypeHelper.IsNullorEmpty(text2))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        builder.AppendFormat(
"{0}:S:{1}:{2}:", text, num, text2.Length);
InBlock.gif                        builder2.Append(text2);
InBlock.gif                        num 
+= text2.Length;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                keys 
= builder.ToString();
InBlock.gif                values 
= builder2.ToString();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object ConvertSOAPToObject(string xml, Type objType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MemoryStream serializationStream 
= null;
InBlock.gif            
object obj2;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                IFormatter formatter 
= new SoapFormatter();
InBlock.gif                serializationStream 
= new MemoryStream(Encoding.Default.GetBytes(xml));
InBlock.gif                obj2 
= formatter.Deserialize(serializationStream);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (serializationStream != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    serializationStream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return obj2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static byte[] ConvertToBytes(object objectToConvert)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
byte[] buffer = null;
InBlock.gif            
if (CanBinarySerialize)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BinaryFormatter formatter 
= new BinaryFormatter();
InBlock.gif                
using (MemoryStream serializationStream = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    formatter.Serialize(serializationStream, objectToConvert);
InBlock.gif                    serializationStream.Position 
= 0;
InBlock.gif                    buffer 
= new byte[serializationStream.Length];
InBlock.gif                    serializationStream.Read(buffer, 
0, buffer.Length);
InBlock.gif                    serializationStream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return buffer;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static byte[] ConvertToBytes(string s)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Convert.FromBase64String(s);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static NameValueCollection ConvertToNameValueCollection(string keys, string values)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            NameValueCollection values2 
= new NameValueCollection();
InBlock.gif            
if (((keys != null&& (values != null)) && ((keys.Length > 0&& (values.Length > 0)))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
char[] separator = new char[] dot.gif':' };
InBlock.gif                
string[] textArray = keys.Split(separator);
InBlock.gif                
for (int i = 0; i < (textArray.Length / 4); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
int startIndex = int.Parse(textArray[(i * 4+ 2], CultureInfo.InvariantCulture);
InBlock.gif                    
int length = int.Parse(textArray[(i * 4+ 3], CultureInfo.InvariantCulture);
InBlock.gif                    
string text = textArray[i * 4];
InBlock.gif                    
if (((textArray[(i * 4+ 1== "S"&& (startIndex >= 0)) && (values.Length >= (startIndex + length)))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        values2[text] 
= values.Substring(startIndex, length);
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return values2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object ConvertToObject(byte[] byteArray)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object obj2 = null;
InBlock.gif            
if ((CanBinarySerialize && (byteArray != null)) && (byteArray.Length > 0))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BinaryFormatter formatter 
= new BinaryFormatter();
InBlock.gif                
using (MemoryStream serializationStream = new MemoryStream())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    serializationStream.Write(byteArray, 
0, byteArray.Length);
InBlock.gif                    serializationStream.Position 
= 0;
InBlock.gif                    
if (byteArray.Length > 4)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        obj2 
= formatter.Deserialize(serializationStream);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    serializationStream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return obj2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object ConvertToObject(string xml, Type objectType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object obj2 = null;
InBlock.gif            
if (!TypeHelper.IsNullorEmpty(xml))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (StringReader textReader = new StringReader(xml))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    obj2 
= new XmlSerializer(objectType).Deserialize(textReader);
InBlock.gif                    textReader.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return obj2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object ConvertToObject(XmlNode node, Type objectType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object obj2 = null;
InBlock.gif            
if (node != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
using (StringReader textReader = new StringReader(node.OuterXml))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    obj2 
= new XmlSerializer(objectType).Deserialize(textReader);
InBlock.gif                    textReader.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return obj2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string ConvertToSOAPString(object obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MemoryStream serializationStream 
= null;
InBlock.gif            
string text;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                serializationStream 
= new MemoryStream();
InBlock.gif                IFormatter formatter 
= new SoapFormatter();
InBlock.gif                formatter.Serialize(serializationStream, obj);
InBlock.gif                
int count = (int)serializationStream.Length;
InBlock.gif                
byte[] buffer = new byte[count];
InBlock.gif                serializationStream.Seek((
long)0, SeekOrigin.Begin);
InBlock.gif                serializationStream.Read(buffer, 
0, count);
InBlock.gif                UTF8Encoding encoding 
= new UTF8Encoding();
InBlock.gif                text 
= encoding.GetString(buffer).Trim();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (serializationStream != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    serializationStream.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return text;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string ConvertToString(byte[] arr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return Convert.ToBase64String(arr);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string ConvertToString(object objectToConvert)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string text = null;
InBlock.gif            
if (objectToConvert != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlSerializer serializer 
= new XmlSerializer(objectToConvert.GetType());
InBlock.gif                
using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    serializer.Serialize((TextWriter)writer, objectToConvert);
InBlock.gif                    text 
= writer.ToString();
InBlock.gif                    writer.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return text;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static object LoadBinaryFile(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!File.Exists(path))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
using (FileStream input = new FileStream(path, FileMode.Open, FileAccess.Read))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BinaryReader reader 
= new BinaryReader(input);
InBlock.gif                
byte[] buffer = new byte[input.Length];
InBlock.gif                reader.Read(buffer, 
0, (int)input.Length);
InBlock.gif                
return ConvertToObject(buffer);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static bool SaveAsBinary(object objectToSave, string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((objectToSave != null&& CanBinarySerialize)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
byte[] buffer = ConvertToBytes(objectToSave);
InBlock.gif                
if (buffer != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
using (FileStream output = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
using (BinaryWriter writer = new BinaryWriter(output))
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            writer.Write(buffer);
InBlock.gif                            
return true;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static void SaveAsXML(object objectToConvert, string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (objectToConvert != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                XmlSerializer serializer 
= new XmlSerializer(objectToConvert.GetType());
InBlock.gif                
using (StreamWriter writer = new StreamWriter(path))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    serializer.Serialize((TextWriter)writer, objectToConvert);
InBlock.gif                    writer.Close();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string XMLDecode(string sTmp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] numArray = new int[] dot.gif0x26600x3e0x220x3d0x27 };
InBlock.gif            
for (int i = 0; i < numArray.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sTmp 
= sTmp.Replace("" + numArray[i].ToString() + ";", ((char)numArray[i]).ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sTmp;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static string XMLEncode(string sTmp)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
int[] numArray = new int[] dot.gif0x26600x3e0x220x3d0x27 };
InBlock.gif            
for (int i = 0; i < numArray.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sTmp 
= sTmp.Replace(((char)numArray[i]).ToString(), "" + numArray[i].ToString() + ";");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sTmp;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
posted on 2007-04-30 20:53 黄尚 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/afxcn/archive/2007/04/30/733665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值