Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery8-Oct-08 10:34
Mark Salsbery8-Oct-08 10:34 
AnswerRe: BinaryFormatter: Serializing an object's base class Pin
Giorgi Dalakishvili8-Oct-08 7:35
mentorGiorgi Dalakishvili8-Oct-08 7:35 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
mav.northwind8-Oct-08 23:48
mav.northwind8-Oct-08 23:48 
AnswerRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery8-Oct-08 7:59
Mark Salsbery8-Oct-08 7:59 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
mav.northwind8-Oct-08 8:27
mav.northwind8-Oct-08 8:27 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery8-Oct-08 9:37
Mark Salsbery8-Oct-08 9:37 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
mav.northwind8-Oct-08 10:05
mav.northwind8-Oct-08 10:05 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery8-Oct-08 11:01
Mark Salsbery8-Oct-08 11:01 
I was bored, so I tried it. Works great Smile | :)
	[Serializable]
	public class Animal
	{
		public string str
		{
			get;
			set;
		}
	}

	[Serializable]
	public class Dog : Animal
	{
		[NonSerialized]  <code>//<-- Optional!</code>
		public Int32 A;

		[NonSerialized]  <code>//<-- Optional!</code>
		public Int32 B;

		public Dog()
		{
			A = 5;
			B = 10;
		}
	}

	sealed class DogToAnimalDeserializationBinder : SerializationBinder
	{
		public override Type BindToType(string assemblyName, string typeName)
		{
			Type typeToDeserialize = null;

			String assemVer1 = Assembly.GetExecutingAssembly().FullName;
			String typeVer1 = "Dog";

			if (assemblyName == assemVer1 && typeName == typeVer1)
			{
				typeName = "Animal";
			}

			// The following line of code returns the type.
			typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
				typeName, assemblyName));

			return typeToDeserialize;
		}
	}

...

Dog dog = new Dog();
dog.A = 3;
dog.B = 5;
dog.str = "Animal String";

MemoryStream MemStream = new MemoryStream();

// Serialize a Dog object
BinaryFormatter Serializer = new BinaryFormatter();
Serializer.Serialize(MemStream, dog);

MemStream.Seek(0, SeekOrigin.Begin);

// Deserialize as an Animal object
Serializer.Binder = new DogToAnimalDeserializationBinder();
Animal animal = (Animal)Serializer.Deserialize(MemStream); 


Mark Salsbery
Microsoft MVP - Visual C++

Java | [Coffee]

GeneralRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery8-Oct-08 12:21
Mark Salsbery8-Oct-08 12:21 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
mav.northwind8-Oct-08 23:46
mav.northwind8-Oct-08 23:46 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery9-Oct-08 5:09
Mark Salsbery9-Oct-08 5:09 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
mav.northwind9-Oct-08 8:42
mav.northwind9-Oct-08 8:42 
GeneralRe: BinaryFormatter: Serializing an object's base class Pin
Mark Salsbery9-Oct-08 8:54
Mark Salsbery9-Oct-08 8:54 
QuestionWhat do I put in a VSTO Outlook Add In shutdown handler? Pin
justastupidgurl8-Oct-08 2:59
justastupidgurl8-Oct-08 2:59 
QuestionI need free C# TreeList( or TreeView) code that will run on the web form Pin
inerp8-Oct-08 1:30
inerp8-Oct-08 1:30 
AnswerRe: I need free C# TreeList( or TreeView) code that will run on the web form Pin
J4amieC8-Oct-08 1:39
J4amieC8-Oct-08 1:39 
AnswerRe: I need free C# TreeList( or TreeView) code that will run on the web form Pin
Giorgi Dalakishvili8-Oct-08 1:44
mentorGiorgi Dalakishvili8-Oct-08 1:44 
AnswerRe: I need free C# TreeList( or TreeView) code that will run on the web form Pin
leppie8-Oct-08 1:53
leppie8-Oct-08 1:53 
AnswerRe: I need free C# TreeList( or TreeView) code that will run on the web form Pin
Muhammad Gouda8-Oct-08 2:01
Muhammad Gouda8-Oct-08 2:01 
AnswerRe: I need free C# TreeList( or TreeView) code that will run on the web form Pin
#realJSOP8-Oct-08 2:59
mve#realJSOP8-Oct-08 2:59 
Questionstatic Object Pin
Deepak the Cool8-Oct-08 1:20
Deepak the Cool8-Oct-08 1:20 
AnswerRe: static Object Pin
Colin Angus Mackay8-Oct-08 2:08
Colin Angus Mackay8-Oct-08 2:08 
QuestionCentralized exception handler and logger Pin
matixsc8-Oct-08 0:57
professionalmatixsc8-Oct-08 0:57 
AnswerRe: Centralized exception handler and logger Pin
Giorgi Dalakishvili8-Oct-08 1:14
mentorGiorgi Dalakishvili8-Oct-08 1:14 
AnswerRe: Centralized exception handler and logger Pin
matixsc8-Oct-08 2:16
professionalmatixsc8-Oct-08 2:16 

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.