tags:

views:

51

answers:

3

Hi there, a little while ago i was reading an article about a series of class that were created that handled the conversion of strings into a generic type. Below is a mock class structure. Basically if you set the StringValue it will perform some conversion into type T

public class MyClass<T>
{
    public string StringValue {get;set;}
    public T Value {get;set;}
}

I cannot remember the article that i was reading, or the name of the class i was reading about. Is this already implemented in the framework? Or shall i create my own?

+1  A: 

This does not exist in the .NET framework. You'd have to create your own.

Timothy Khouri
I have seen an example of this on the net, i'm sure it was on MSDN, it might be part of an opensource project... ah.. i just cant rememeber...
Rohan West
A: 

I don't remember anything like that, but if it did exist it would almost certainly be an abstract class or interface, still requiring you to implement the conversion logic yourself. There's really no possible way for Microsoft to write code that can accept a string representation of classes that haven't even been written yet and just know how to properly construct that class.

When you think about it that way, the abstract functionality is already available in Func<string, T> or one of the many serialization formats out there (xml, json, protobuf, etc).

Joel Coehoorn
Actually, you could make your own generic class that would attempt to handle converting any string to any class passed in as type "T" generically... the answer to a similar question I asked shows how: http://stackoverflow.com/questions/312858/how-can-i-convert-types-at-runtime
Timothy Khouri