System.Xml.XmlSerializer and Classes with Generic Type Parameters

  • Thread starter Thread starter Hoss
  • Start date Start date
H

Hoss

Discovered this today after alot of head-scratching, just thought I
would toss it out into cyberspace in hopes that it would help someone.

This of course, will not compile

public class Woot`Class - notice the backtick
{

}

However, you can in fact create a type with a name like that -
including backticks, [, _+, - and other illegal characters using
Reflection.Emit. If you then do the following

new System.Xml.XmlSerializer(TypeWithAFunkyName)

you will get the marvelously ambiguous and utterly worthless error
message, "Input string was not in a correct format."

So, if you ever get that message from the XmlSerializer constructor -
consider the actual name of the type you are passing in !
 
That's the thing, they are not illegal characters. If you look at the
reflection namespace, as specifically the FullName AssemblyQualifiedFullName
properties on the Type class, you will that is how generic type parameters
are represented through reflection (using the back tick).

They are just not valid in C#, as C# uses a different syntax for
specifying generic type parameters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top