473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1354
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...

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.