473,491 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Serialization - bug in framework.

I know you should be careful before blaming internal frameworks,
but this definitely does look like an internal bug.

When serializing a ChildClass that inherits a BaseClass containing
a struct that contains an object (phew..) the serialization fails.
Error message: "FieldInfo does not match the target Type"

I can serialize the struct, serialize the baseclass (containing the struct),
serialize the childclass when its empty, but _not_ when it contains the
struct.

Paste the following code in a new console application:

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SharpSerializeBug
{
class MyClass
{
[Serializable]
public class BaseClass
{
public ContainedStruct Contained;
}

[Serializable]
public class ChildClass : BaseClass{}

[Serializable]
public struct ContainedStruct
{
int[] SomeObject;

public ContainedStruct(int[] anArray)
{
this.SomeObject = anArray;
}
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
BaseClass bs = new BaseClass();
ChildClass cs = new ChildClass();
ContainedStruct contained = new ContainedStruct(new int[] {1,2,3});

TrySerialize("Empty Base Class", bs);
TrySerialize("Empty Child Class", cs);
TrySerialize("Contained struct", contained);

bs.Contained = contained;
cs.Contained = contained;

TrySerialize("Non-Empty Base Class", bs);
TrySerialize("Non-Empty Child Class", cs);

Console.ReadLine();
}

static public void TrySerialize(string message, object o)
{
try
{
IFormatter formatter = new BinaryFormatter();
// Serialize qr
Stream stream = new FileStream("MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None);
formatter.Serialize(stream, o);
stream.Close();

// Deserialize qr
stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read,
FileShare.Read);
object obj = formatter.Deserialize(stream);
stream.Close();

Console.WriteLine("Serialization of " + message + " succeeded.");
}
catch(Exception exp)
{
Console.WriteLine("Serialization of " + message + " failed: " +
exp.Message);
}
}
}

}

Jul 21 '05 #1
1 1356
Just a random thought: Doesn't the object to serialized have to have
a default parameterless constructor? I ran into issues when
serializing and that was the problem although I was not getting the
same error message you are getting.

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2156
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
2
1955
by: reycri | last post by:
I have a .Net class (a collection) that already supports serialization. It implements ISerializable... Now, I need it to also support the COM interface IPersistStream. Among other things, I need...
0
1588
by: HakonB | last post by:
Hi all I get an exception when trying to deserialize a simple configuration file using XML Serialization. The very long stacktrace can be seen at the bottom of this message. I've see other...
5
2811
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
3
2434
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
7461
by: Brian Keating | last post by:
wonder if anyone can help me here, i've a framework 1.1 dataset which i serialize in framework 1.1 and deserialize in framework 2.0. This is fine, problem is that i want to modify some of the...
0
1203
by: nickdu | last post by:
We have the need to persist data to a configuration database. We want the format of this data to be XML. There are two approaches that come to mind that seem to fit the bill. Below are the two...
1
2434
by: Tim Anderson | last post by:
I have an app that uses the SOAP serializer to serialize and deserialize a object of a certain class to a file. The class implements ISerializable. From time to time it is necessary to add or...
2
9456
by: =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno | last post by:
Hello. I have this kind of object: class classA { int x; classA z; public int X { get { return this.x; }
4
11371
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3086
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.