I have:
- data type (all types are values types) as
string(for example: ‘System.Boolean’) - data value as
string(for example: ‘true’)
I have to get:
- instance of value type
- this instance should be initialized by that value
I have started from
object v = Activator.CreateInstance(System.Type.GetType('type as string', true, true),..);
and I don’t know how to initialize v to value: how can I convert dynamically value as string to bool (or other value types) and assign it to v?
Thank you.
Have you tried using
Convert.ChangeType?That will only work for certain target types, but that may be enough for you.
EDIT: Ultimately, you’ll need a set of types you want to support. You may want to have some sort of
Dictionary<Type, Func<string, object>>and a method which is able to disguise how you handle more general cases (such as enums).