Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi,

I get the usual error but this is the line it's supposed to occur on:
C#
public string Serialize()
{
  var serializer = new XmlSerializer(GetType()); //<<< This line
  TextWriter writer = new StringWriter();
  serializer.Serialize(writer, this);

  return writer.ToString();
}


I don't see how that makes any sense. My debugger is unable to unwind but I have checked the values in QuickWatch. Nothing on that line is null, but then again I can't see what on that line can be null, can it?

This isn't the first time I've seen this but now it's happening every time :S

Any ideas how I can proceed?

Thanks ^_^
Posted
Comments
Michael_Davies 17-Jun-15 12:38pm    
not sure but shouldn't GetType have a parameter or sourced from a variable as in passing an object then object.GetType() returns its type?
Andy Lanng 17-Jun-15 12:41pm    
Sorry, I should have said
This is a method in an abstract class so it could read this.GetType()
Richard Deeming 17-Jun-15 14:16pm    
I suspect the error is actually thrown deep in the bowels of the XmlSerializer class - it's doing quite a lot of work to generate a temporary assembly for the serialization.

You can apparently debug the process[^] by changing the app.config file and stepping in to the temporary assemblies.

Alternatively, use the sgen.exe tool[^], or the setting in the project properties, to pre-generate the XML serializer assembly.
Andy Lanng 17-Jun-15 15:15pm    
cool - i didn't know you could do that - I'll report back tomorrow ^_^

1 solution

Sounds like some strange behaviour. Could you check the type of this like in the code below? Put a breakpoint in the vs and check out what thisType is. Maybe it gives a clue.
C#
public string Serialize()
{
  Type thisType = this.GetType();
  [BREAKPOINT] var serializer = new XmlSerializer(thisType); //<<< This line
  TextWriter writer = new StringWriter();
  serializer.Serialize(writer, this);

  return writer.ToString();
}


Good luck!
 
Share this answer
 
v2
Comments
Andy Lanng 17-Jun-15 12:52pm    
Ok. I'll have to check tomorrow now, but I'll update you then
E.F. Nijboer 17-Jun-15 12:55pm    
Great :)
Andy Lanng 18-Jun-15 4:37am    
HA typical. I was pretty sure this was some sort of race condition. With that extra step, it doesn't seem to be occurring anymore!

That is: It won't occur until I release to prod >_<
Andy Lanng 18-Jun-15 5:19am    
Gah - That error has stopped but 50 more show up in it's place. Almost nothing I have written is thread-safe >_<

I'm gonna have to step back and rethink my approach here :(
E.F. Nijboer 18-Jun-15 12:13pm    
Nice you already found it. Good luck hardening your code and making it thread-safe!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900