Click here to Skip to main content
15,911,315 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Question with Generics Pin
darkelv22-Feb-07 0:42
darkelv22-Feb-07 0:42 
GeneralRe: C# Question with Generics Pin
malharone22-Feb-07 7:49
malharone22-Feb-07 7:49 
GeneralRe: C# Question with Generics Pin
malharone27-Feb-07 11:13
malharone27-Feb-07 11:13 
QuestionStart reading at the end of a file? Pin
TheJudeDude21-Feb-07 14:05
TheJudeDude21-Feb-07 14:05 
AnswerRe: Start reading at the end of a file? Pin
Shajeel21-Feb-07 18:54
Shajeel21-Feb-07 18:54 
GeneralRe: Start reading at the end of a file? Pin
TheJudeDude22-Feb-07 3:31
TheJudeDude22-Feb-07 3:31 
GeneralRe: Start reading at the end of a file? Pin
Luc Pattyn22-Feb-07 8:08
sitebuilderLuc Pattyn22-Feb-07 8:08 
QuestionISerializable.GetObjectData Method Example Pin
Gywox21-Feb-07 11:53
Gywox21-Feb-07 11:53 
I have some problems with microsoft´s example under void GetObjectData(SerializationInfo info, StreamingContext context) in MSDN VS5. I have executed the code and it´s running, but when I remove formatter.Serialize(fs, a1) and try to Deserialize the binary file, it fails.
Have I misunderstod the example or What is wrong? Can I fix the code I need to serialize a singleton.
Best regards, Gywox

Modified Example:
<br />
using System;<br />
using System.Web;<br />
using System.IO;<br />
using System.Collections;<br />
using System.Runtime.Serialization.Formatters.Binary;<br />
using System.Runtime.Serialization;<br />
using System.Security.Permissions;<br />
<br />
[Serializable]<br />
public sealed class Singleton : ISerializable<br />
{<br />
	private static readonly Singleton theOneObject = new Singleton();<br />
	private string someString_value;<br />
	private Int32 someNumber_value;<br />
<br />
	public string SomeString<br />
	{<br />
		get<br />
		{<br />
			return someString_value;<br />
		}<br />
		set<br />
		{<br />
			someString_value = value;<br />
		}<br />
	}<br />
<br />
	public Int32 SomeNumber<br />
	{<br />
		get<br />
		{<br />
			return someNumber_value;<br />
		}<br />
		set<br />
		{<br />
			someNumber_value = value;<br />
		}<br />
	}<br />
<br />
	private Singleton()<br />
	{<br />
	}<br />
<br />
	public static Singleton GetSingleton()<br />
	{<br />
		return theOneObject;<br />
	}<br />
<br />
	[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]<br />
	void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)<br />
	{<br />
		info.SetType(typeof(SingletonSerializationHelper));<br />
	}<br />
}<br />
<br />
<br />
[Serializable]<br />
internal sealed class SingletonSerializationHelper : IObjectReference<br />
{<br />
	public Object GetRealObject(StreamingContext context)<br />
	{<br />
		return Singleton.GetSingleton();<br />
	}<br />
}<br />
<br />
class App<br />
{<br />
	[STAThread]<br />
	static void Main()<br />
	{<br />
		FileStream fs = new FileStream("DataFile.dat", FileMode.OpenOrCreate);<br />
<br />
		try<br />
		{<br />
			BinaryFormatter formatter = new BinaryFormatter();<br />
			//Singleton[] a1 = { Singleton.GetSingleton(), Singleton.GetSingleton() };<br />
			//a1[0].SomeString = "This is a string field";<br />
			//a1[0].SomeNumber = 123;<br />
			//a1[1].SomeString = "This is a string field";<br />
			//a1[1].SomeNumber = 123;<br />
			//Console.WriteLine("Do both array elements refer to the same object? " + (a1[0] == a1[1]));<br />
			//formatter.Serialize(fs, a1);<br />
			//fs.Position = 0;<br />
<br />
			Singleton[] a2 = (Singleton[])formatter.Deserialize(fs);<br />
			Console.WriteLine("Do both array elements refer to the same object? " + (a2[0] == a2[1]));<br />
			//Console.WriteLine("Do all array elements refer to the same object? " + (a1[0] == a2[0]));<br />
			Console.WriteLine(a2[0].SomeString + "  " + a2[0].SomeNumber);<br />
			Console.WriteLine(a2[1].SomeString + "  " + a2[1].SomeNumber);<br />
		}<br />
		catch (SerializationException e)<br />
		{<br />
			Console.WriteLine("Failed to serialize. Reason: " + e.Message);<br />
			throw;<br />
		}<br />
		finally<br />
		{<br />
			fs.Close();<br />
		}<br />
	}<br />
}

AnswerThats a lot of code Pin
Ennis Ray Lynch, Jr.21-Feb-07 12:48
Ennis Ray Lynch, Jr.21-Feb-07 12:48 
GeneralRe: Thats a lot of code Pin
Gywox21-Feb-07 23:23
Gywox21-Feb-07 23:23 
GeneralRe: Thats a lot of code Pin
Ennis Ray Lynch, Jr.22-Feb-07 2:45
Ennis Ray Lynch, Jr.22-Feb-07 2:45 
Questionmodal popup Pin
netJP12L21-Feb-07 10:08
netJP12L21-Feb-07 10:08 
QuestionChanging the Size of a Panel [modified] Pin
aei_totten21-Feb-07 9:23
aei_totten21-Feb-07 9:23 
AnswerRe: Changing the Size of a Panel Pin
Luc Pattyn21-Feb-07 10:53
sitebuilderLuc Pattyn21-Feb-07 10:53 
QuestionVisual Studio 2003 and Crystal Report Viewer Pin
Saamir21-Feb-07 9:07
Saamir21-Feb-07 9:07 
QuestionMouse following movements C# Pin
Dang3russ21-Feb-07 8:33
Dang3russ21-Feb-07 8:33 
AnswerRe: Mouse following movements C# Pin
led mike21-Feb-07 8:55
led mike21-Feb-07 8:55 
Questionhow to save xml file via web service Pin
BraveShogun21-Feb-07 7:20
BraveShogun21-Feb-07 7:20 
AnswerRe: how to save xml file via web service Pin
Ennis Ray Lynch, Jr.21-Feb-07 12:51
Ennis Ray Lynch, Jr.21-Feb-07 12:51 
QuestionDefault Webservice URL Pin
kmuthuk21-Feb-07 6:30
kmuthuk21-Feb-07 6:30 
AnswerRe: Default Webservice URL Pin
Ennis Ray Lynch, Jr.21-Feb-07 12:55
Ennis Ray Lynch, Jr.21-Feb-07 12:55 
GeneralNeed C++ Library in C# Class Library Pin
ThatsAlok21-Feb-07 6:17
ThatsAlok21-Feb-07 6:17 
GeneralRe: Need C++ Library in C# Class Library Pin
Not Active21-Feb-07 6:19
mentorNot Active21-Feb-07 6:19 
GeneralRe: Need C++ Library in C# Class Library Pin
ThatsAlok21-Feb-07 7:23
ThatsAlok21-Feb-07 7:23 
GeneralRe: Need C++ Library in C# Class Library Pin
Not Active21-Feb-07 7:37
mentorNot Active21-Feb-07 7:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.