// Copyright 2005 by Omar Al Zabir. All rights are reserved.
//
// If you like this code then feel free to go ahead and use it.
// The only thing I ask is that you don't remove or alter my copyright notice.
//
// Your use of this software is entirely at your own risk. I make no claims or
// warrantees about the reliability or fitness of this code for any particular purpose.
// If you make changes or additions to this code please mark your code as being yours.
//
// website http://www.oazabir.com, email OmarAlZabir@gmail.com, msn oazabir@hotmail.com
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;
using System.Collections;
namespace RSSCommon.Helper{
/// <summary>
/// Utility methods
/// </summary>
public class SerializationHelper
{
public const string RESOURCE_ASSEMBLY_PREFIX = "RSSFeederResources.";
public static XmlWriter Serialize( Stream stream, object o )
{
XmlTextWriter writer = new XmlTextWriter( stream, System.Text.Encoding.UTF8 );
XmlSerializer serializer = new XmlSerializer( o.GetType() );
serializer.Serialize( writer, o );
return writer;
}
public static XmlWriter Serialize( Stream stream, ArrayList array, Type type )
{
XmlTextWriter writer = new XmlTextWriter( stream, System.Text.Encoding.UTF8 );
XmlSerializer serializer = new XmlSerializer( typeof(ArrayList), new Type[] { type } );
serializer.Serialize( writer, array );
return writer;
}
public static object Deserialize( Stream stream, Type t )
{
XmlTextReader reader = new XmlTextReader( stream );
XmlSerializer serializer = new XmlSerializer( t );
object o = serializer.Deserialize( reader );
return o;
}
public static ArrayList DeserializeArraylist( Stream stream, Type t )
{
XmlTextReader reader = new XmlTextReader( stream );
XmlSerializer serializer = new XmlSerializer( typeof( ArrayList ), new Type [] { t } );
ArrayList list = (ArrayList) serializer.Deserialize( reader );
return list;
}
public static Stream GetStream( string name )
{
return GetResourceAssembly().GetManifestResourceStream(RESOURCE_ASSEMBLY_PREFIX + name);
}
public static System.Drawing.Icon GetIcon( string name )
{
using( Stream stream = GetStream( name ) )
{
return new System.Drawing.Icon( stream );
}
}
public static Assembly GetResourceAssembly()
{
return Assembly.LoadFrom("RSSFeederResources.dll");
}
public static void WriteEmbeddedFile( string name, string fileName )
{
using( Stream stream = GetStream( name ) )
{
FileInfo file = new FileInfo( fileName );
using( FileStream fileStream = file.Create() )
{
byte [] buf = new byte[ 1024 ];
int size;
while( (size = stream.Read( buf, 0, 1024 )) > 0 )
{
fileStream.Write( buf, 0, size );
}
}
}
}
public static void CopyProperties( object source, object destination )
{
PropertyInfo [] properties = source.GetType().GetProperties();
foreach( PropertyInfo prop in properties )
{
if( prop.CanWrite )
prop.SetValue( destination, prop.GetValue( source, null ), null );
}
}
}
}
|