extending Convert.ChangeType to produce user-defined types on request

Viewed 17558

Given the class:

public class Foo
{
    public string Name { get; set; }
}

Is it possible to have a Foo instance created from a string through Convert.ChangeType:

Type type = typeof(Foo);
object value = "one";

value = System.Convert.ChangeType(value, type);

This is how a 3rd party API is attempting to rebuild objects. Someone mentioned this is possible with implicit operators, but from my understanding that will let me do the following, not create the object:

Foo foo = new Foo() { Name = "one" };
string fooAsString = foo;  // implicit conversion -- no cast needed

Is there a way to create the object this way? Also, I do have the ability to change the Convert.ChangeType if there is another way to do this.

Update: The reason I am asking is because it throws and exception:

Invalid cast from 'System.String' to 'JibbaJabba+Foo'.

and adding the operator did not resolve the issue.

4 Answers
Related