SaloS

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 classes and cannot see how it is implemented.
I would not like to write my own concertion class.


Re: Visual C# General How to convert from string to object of any type

KJacquemin

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/





Re: Visual C# General How to convert from string to object of any type

SaloS

There is no code there




Re: Visual C# General How to convert from string to object of any type

RichardSny

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.




Re: Visual C# General How to convert from string to object of any type

SaloS

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.




Re: Visual C# General How to convert from string to object of any type

RichardSny

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.




Re: Visual C# General How to convert from string to object of any type

timvw

How about using Activator.CreateInstance


Code Snippet

namespace 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 Snippet
public 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);
}
}
}








Re: Visual C# General How to convert from string to object of any type

SaloS

>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.