views:

551

answers:

3

Hmmm I'm not sure if i titled this question properly or am asking it properly, but here goes.

I've got serialized objects (in XML) stored in a database, along with a string/varchar indicating the type.

Right now i am doing this: (because i have a finite number of different types)

Dim deserializer as XmlSerializer
If datatable("type") = "widget1" then 
     deserializer = new XmlSerializer(GetType(Widget1))
elseif datatable("type") = "widget2" then 
     deserializer = new XmlSerializer(GetType(Widget2))
...

i'd like to do something like

Dim deserializer as XmlSerializer
deserializer = new XmlSerializer(MagicallyConvertToSystemDotType(datatable("type"))

Am i barking up the wrong tree here?

+2  A: 

Have you tried using Type.GetType? This takes a string parameter and returns a type for that name. You may have to give it additional information about the simple name "widget" and more along the lines of a full name. But it appears from your sample they should all have the same namespace so that shouldn't be a big hurdle.

JaredPar
A: 

The other option if you want an actual keyword Type to work with, and not a variable type is using something like (sorry I'm using C# and am too tired to do the VB conversion):

method in XmlSerializer like Deserialize(typestring, object);
method in XmlSerializer like Deserialize<T>(object);


public void Deserialize(string typestring, object obj)
{
  MethodInfo deserialize = typeof(XmlSerializer)
      .GetMethod("Deserialize", BindingFlags.Instance | BindingFlags.Public)
      .MakeGenericMethod(new Type[] { Type.GetType(typestring) });
  deserialize.Invoke(this, new[] { obj });
}
Graphain
A: 

Specifically, I think you're looking for this code here (NOTE: I don't work much in VB.Net, so I hope everything there is syntactically correct):

VB.Net:

// Get the type of object being deserialized.
Dim t as Type = Type.GetType(typeNameString);
// Make a new instance of the object.
Dim o as Object = Activator.CreateInstance(t);

C#:

// Get the type of object being deserialized.
Type t = Type.GetType(typeNameString);
// Make a new instance of the object.
object o = Activator.CreateInstance(t);

Edit (26 Oct, 2009, 15:10 GMT-0600): The Type.GetType(string typeNameString) method does not always recognize types as simply their fully qualified name. It would be in your best interest to be sure and include as much information as you can in your parameter string, as follows:

VB.Net/C#:

typeNameString = objectSerialized.GetType().Namespace + ", " + objectSerialized.GetType().Name + ", " + objectSerialized.GetType().Assembly.FullName

Less specifically, I just had the same problem, and after a lot of research, I finally came up with a nice solution for handling all most of this dynamically. I've posted the entire source code to a class capable of serializing and deserializing objects of any type not containing generics or arrays using Reflection. Feel free to take it and use it as your own. If anyone decides to add the handling for generics and arrays, please send me an updated copy so I can post it back on my blog (and you'll get an honorable mention ;-)...). It will serialize everything recursively, and has some special coding in there for enums as well.

Take a look and see if that covers everything you're looking for at:

http://maxaffinity.blogspot.com/2009/10/serialize-objects-manually.html

~md5sum~

Edit (27 Oct, 2009 14:38 GMT-0600): Corrected some misinformation about the class available from my blog.

md5sum