473,396 Members | 1,852 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,396 software developers and data experts.

Deserialization problem. Can't set value of new field

Joe
Hello,

I'm trying to deserialize my objects (which derive from CollectionBase)
using the special constructor. Everything works fine until I go to set the
value. I get the following error:
"Object type cannot be converted to target type."

The property Value of newField = 'ArrayList' and val also shows as
ArrayList'.

What could be wrong?

public MyCollection(SerializationInfo info, StreamingContext context)
{
Type entityType = this.GetType();

string fieldName = string.Empty;

foreach (SerializationEntry entry in info)
{
fieldName = entry.Name;

if (fieldName.IndexOf('+') > -1 )
{
string []names = fieldName.Split('+');
string baseType = names[0];

fieldName = names[1];

while (entityType.Name != baseType)
entityType = entityType.BaseType;
}
else
entityType = this.GetType();

MemberInfo []members = entityType.GetMember(fieldName,
MemberTypes.Field, BindingFlags.NonPublic |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

if (members.Length > 0)
{
FieldInfo newField = members[0] as FieldInfo;

object val = entry.Value;

if (val != null)
{
if (!newField.FieldType.IsInstanceOfType(val) )
val = Convert.ChangeType(val, newField.FieldType);
}

try
{
// throws error - "Object type cannot be converted to target type."
newField.SetValue(entry, val);
}
catch (Exception ex)
{
int stop = 0;
}
}
}
}

Many thanks,
Joe
Nov 17 '05 #1
2 2769
Joe
I sort of worked around this by creating a Surrogate class for the
BinaryFormatter.

My next problem is to deal with new members which are initialized in the
constructor.

I added a new field to my class (a hashtable) and now when the class is
deserialized that member doesn't get initialized. Is there a way to handle
this using the Surrogate?
"Joe" <jb*******@noemail.noemail> wrote in message
news:e0****************@TK2MSFTNGP12.phx.gbl...
Hello,

I'm trying to deserialize my objects (which derive from CollectionBase)
using the special constructor. Everything works fine until I go to set the
value. I get the following error:
"Object type cannot be converted to target type."

The property Value of newField = 'ArrayList' and val also shows as
ArrayList'.

What could be wrong?

public MyCollection(SerializationInfo info, StreamingContext context)
{
Type entityType = this.GetType();

string fieldName = string.Empty;

foreach (SerializationEntry entry in info)
{
fieldName = entry.Name;

if (fieldName.IndexOf('+') > -1 )
{
string []names = fieldName.Split('+');
string baseType = names[0];

fieldName = names[1];

while (entityType.Name != baseType)
entityType = entityType.BaseType;
}
else
entityType = this.GetType();

MemberInfo []members = entityType.GetMember(fieldName,
MemberTypes.Field, BindingFlags.NonPublic |
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

if (members.Length > 0)
{
FieldInfo newField = members[0] as FieldInfo;

object val = entry.Value;

if (val != null)
{
if (!newField.FieldType.IsInstanceOfType(val) )
val = Convert.ChangeType(val, newField.FieldType);
}

try
{
// throws error - "Object type cannot be converted to target type."
newField.SetValue(entry, val);
}
catch (Exception ex)
{
int stop = 0;
}
}
}
}

Many thanks,
Joe

Nov 17 '05 #2
Joe
Never mind. I can implement IDeserializationCallback.

"Joe" <jb*******@noemail.noemail> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
I sort of worked around this by creating a Surrogate class for the
BinaryFormatter.

My next problem is to deal with new members which are initialized in the
constructor.

I added a new field to my class (a hashtable) and now when the class is
deserialized that member doesn't get initialized. Is there a way to handle
this using the Surrogate?
"Joe" <jb*******@noemail.noemail> wrote in message
news:e0****************@TK2MSFTNGP12.phx.gbl...
Hello,

I'm trying to deserialize my objects (which derive from CollectionBase)
using the special constructor. Everything works fine until I go to set
the value. I get the following error:
"Object type cannot be converted to target type."

The property Value of newField = 'ArrayList' and val also shows as
ArrayList'.

What could be wrong?

public MyCollection(SerializationInfo info, StreamingContext context)
{
Type entityType = this.GetType();

string fieldName = string.Empty;

foreach (SerializationEntry entry in info)
{
fieldName = entry.Name;

if (fieldName.IndexOf('+') > -1 )
{
string []names = fieldName.Split('+');
string baseType = names[0];

fieldName = names[1];

while (entityType.Name != baseType)
entityType = entityType.BaseType;
}
else
entityType = this.GetType();

MemberInfo []members = entityType.GetMember(fieldName,
MemberTypes.Field, BindingFlags.NonPublic |
BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Instance);

if (members.Length > 0)
{
FieldInfo newField = members[0] as FieldInfo;

object val = entry.Value;

if (val != null)
{
if (!newField.FieldType.IsInstanceOfType(val) )
val = Convert.ChangeType(val, newField.FieldType);
}

try
{
// throws error - "Object type cannot be converted to target type."
newField.SetValue(entry, val);
}
catch (Exception ex)
{
int stop = 0;
}
}
}
}

Many thanks,
Joe


Nov 17 '05 #3

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

Similar topics

2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
12
by: yoshijg | last post by:
Is there a way to be notified when the deserialization of an XML is complete, like a callback method? I tried the IDeserializationCallback interface, but that doesnt for XML-Deserialization. ...
5
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being...
3
by: John Glover | last post by:
To whoever can help, I've been having a problem with XML deserialization lately. I needed to serialize and deserialze two objects which inherited from Hashtable. Because IDictionary...
0
by: =?Utf-8?B?UGllcmNlQnJvc25hbg==?= | last post by:
I am using C#(Vs.Net 2003) The error I am getting is... System.Xml.XmlException: This is an unexpected token. The expected token is 'EndElement' I have come to understand that this error is...
1
by: zlf | last post by:
I have a class. public class A { private int id=0; public int Id { get { return id; }
7
by: Andrew | last post by:
Hi, I am using DataContractJsonSerializer to deserialize JSON string in C# objects but I am having a problem. Suppose I have a class: class Item { public ItemId Id { get; set; }
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.