Created
November 22, 2010 06:55
-
-
Save noqisofon/709621 to your computer and use it in GitHub Desktop.
MSDN の ISeriarize インターフェイスの使用例。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Security.Permissions; | |
[assembly: SecurityPermission( SecurityAction.RequestMinimum, Execution = true )] | |
namespace demo.serialize { | |
/// <summary> | |
/// | |
/// </summary> | |
class EntryPoint { | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
public void run(string[] args) { | |
BinaryFormatter binary_formatter = new BinaryFormatter(); | |
Person john = new Person(); | |
john.IdNumber = 1010; | |
john.Name = "John Smith"; | |
FileStream stream = new FileStream( "person.bin", FileMode.OpenOrCreate ); | |
binary_formatter.Serialize( stream, john ); | |
stream.Close(); | |
Console.WriteLine( "original name: {0}, original id: {1}", john.Name, john.IdNumber ); | |
// Deserialize. | |
stream = new FileStream( "person.bin", FileMode.OpenOrCreate ); | |
Person smith = (Person)binary_formatter.Deserialize( stream ); | |
Console.WriteLine( "new name: {0}, new id: {1}", smith.Name, smith.IdNumber ); | |
stream.Close(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="args"></param> | |
static void Main(string[] args) { | |
EntryPoint progn = new EntryPoint(); | |
try { | |
progn.run( args ); | |
} catch ( Exception e ) { | |
Console.WriteLine( "{0}: {1}", e.Message, e.StackTrace ); | |
} finally { | |
Console.Write( "press enter to exit...." ); | |
Console.ReadLine(); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Security.Permissions; | |
namespace demo.serialize { | |
/// <summary> | |
/// | |
/// </summary> | |
[Serializable] | |
public class Person : ISerializable { | |
/// <summary> | |
/// | |
/// </summary> | |
private string name_value_; | |
/// <summary> | |
/// | |
/// </summary> | |
private int id_value_; | |
/// <summary> | |
/// | |
/// </summary> | |
public Person() { } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="info"></param> | |
/// <param name="context"></param> | |
protected Person(SerializationInfo info, StreamingContext context) { | |
if ( info == null ) | |
throw new ArgumentNullException( "info" ); | |
name_value_ = (string)info.GetValue( "alt_name", typeof( string ) ); | |
id_value_ = (int)info.GetValue( "alt_id", typeof( int ) ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public string Name { | |
get { return name_value_; } | |
set { name_value_ = value; } | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
public int IdNumber { | |
get { return id_value_; } | |
set { id_value_ = value; } | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="info"></param> | |
/// <param name="context"></param> | |
[SecurityPermission( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter )] | |
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { | |
if ( info == null ) | |
throw new System.ArgumentNullException( "info" ); | |
info.AddValue( "alt_name", this.Name ); | |
info.AddValue( "alt_id", this.IdNumber ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment