Asked by:
How to convert from string to object of any type

Question
-
I need to convert from string that contains data to object of some type that is passed.
I write some kind of serializer of object to/from collection of strings.
Does someone know class that can get string and destination type and return object of specified type as a result.
I think this functionality must exists but I do not know where because this kind of functionality should be used in XmlSerializer and PropertyGrid.
Unforunatelly I have no access to sources of these classesand cannot see how it is implemented.
I would not like to write my own concertion class.Thursday, August 23, 2007 6:49 AM
All replies
-
To see how XmlSerializer or PropertyGrid works, you should get a copy of Lutz Roeder's Reflector for .Net.
http://www.aisto.com/roeder/dotnet/
Thursday, August 23, 2007 7:08 AM -
There is no code thereThursday, August 23, 2007 8:19 AM
-
Can you give more of an example of what you are looking for? From what I gather you want to take a string, concatonate it with another type, and then return a different type. For the most part I would use the Convert function. However if I am mistaken a bit more description would help in figuring out this issue.Thursday, August 23, 2007 12:57 PM
-
I need something like
object ConvertFromString(string sourceString, Type destinationType)
Convert require either IConvertable interface or converting to static type (i.e. ToInt, ToDouble and so on). I need dynamic conversion from string.Thursday, August 23, 2007 1:06 PM -
If you already know what the different varaitey of types you are going to convert to you can create a class or function that is just a large switch statement to all the possible types you want to convert to. I know this isn't probably what you are looking for but it is all I can think of right now.Thursday, August 23, 2007 2:10 PM
-
How about using Activator.CreateInstance ?
Code Snippetnamespace Activation
{
class Program
{
static void Main(string[] args)
{
Type t = Type.GetType("Activation.Person");
Person person = (Person)Activator.CreateInstance(t);
Console.Write("{0}Press any key to continue...", Environment.NewLine);
Console.ReadKey();
}
}
class Person
{
}
}
Anyway, since you're talking about serialization i'm thinking that you want something like:
Code Snippetpublic class SimpleSerializer<T>
{
public String Serialize(T o)
{
using (MemoryStream m = new MemoryStream())
{
XmlSerializer s = new XmlSerializer(o.GetType());
s.Serialize(m, o);
return Encoding.UTF8.GetString(m.ToArray());
}
}
public T Deserialize(String xml)
{
using (MemoryStream m = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
XmlSerializer s = new XmlSerializer(typeof(T));
return (T)s.Deserialize(m);
}
}
}
Thursday, August 23, 2007 4:13 PM -
>If you already know what the different varaitey of types you are going to convert to you can create a class or function that is just a large switch statement to all the possible types you want to convert to. I know this isn't probably what you are looking for but it is all I can think of right now.
I've already done this. But I wonder if any .NET build-in mechanism to do this.Friday, August 24, 2007 5:33 AM -
Men, you save my life :) thank's
"Que a Paz de Deus se Faça Entre nós" wilsonci@hotmail.com
Tuesday, March 13, 2012 12:43 PM