As stated, this is too broad and can not be solved generally.
Here are some options:
Type type = Type.GetType(typename);
object o = Activator.CreateInstance(type);
This will create an instance of the type that typename
is describing. It calls the parameterless constructor of that type. (Downside: Not all objects have a parameterless constructor. Further, this does set the state of the object using value
.)
Type type = Type.GetType(typename);
object o = Activator.CreateInstance(type, new[] { value });
This will create an instance of the type that typename
is describing. It calls a constructor of that type that accepts one parameter of type string
. (Downside: Not all objects have such a constructor. For example, Int32
does not have such a constructor so you will experience a runtime exception.)
Type type = Type.GetType(typename);
object o = Convert.ChangeType(value, type);
This will attempt to convert the string value
to an instance of the required type. This can lead to InvalidCastException
s though. For example, Convert.ChangeType("4", typeof(FileStream))
will obviously fail, as it should.
In fact, this last example (create an instance of type FileStream
with its initial state determined by the string "4"
) shows how absurd the general problem is. There are some constructions/conversions that just can not be done.
You might want to rethink the problem you are trying to solve to avoid this morass.