how do i convert string to type? help please!
-
for example if i have a string like this: string str = "System.Data.OleDb.OleDbParameter"; then i have object like this: object obj; the question how do i convert the type of obj to the value of str? thanks so much
Try the static method Type.GetType(string). It will return a Type object that you can manipulate using reflection. I don't know if there is an easy way to do what you want to do. I will research it.
█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
-
Try the static method Type.GetType(string). It will return a Type object that you can manipulate using reflection. I don't know if there is an easy way to do what you want to do. I will research it.
█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
-
Sorry, I thought this would work but it doesn't. I will leave it here in the hopes that it might spark an idea on how to do it.
string stype = "System.Int32";
Type t = Type.GetType(stype);IConvertible converter = (IConvertible)t; //runtime error, Unable to cast object of type
//'System.RuntimeType' to type 'System.IConvertible'.object obj = converter.ToType(typeof(int), CultureInfo.InvariantCulture);
//(or)
int i = (int)converter.ToType(typeof(int), CultureInfo.InvariantCulture);InvariantCulture is a static property defined in the System.Globalization.CultureInfo class. -- modified at 1:35 Friday 20th October, 2006
█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
-
Sorry, I thought this would work but it doesn't. I will leave it here in the hopes that it might spark an idea on how to do it.
string stype = "System.Int32";
Type t = Type.GetType(stype);IConvertible converter = (IConvertible)t; //runtime error, Unable to cast object of type
//'System.RuntimeType' to type 'System.IConvertible'.object obj = converter.ToType(typeof(int), CultureInfo.InvariantCulture);
//(or)
int i = (int)converter.ToType(typeof(int), CultureInfo.InvariantCulture);InvariantCulture is a static property defined in the System.Globalization.CultureInfo class. -- modified at 1:35 Friday 20th October, 2006
█▒▒▒▒▒██▒█▒██ █▒█████▒▒▒▒▒█ █▒██████▒█▒██ █▒█████▒▒▒▒▒█ █▒▒▒▒▒██▒█▒██
-
You're half way there cap'n, you need to use
Activator.CreateInstance
on thatType
object obtained usingType.GetType
-
You're half way there cap'n, you need to use
Activator.CreateInstance
on thatType
object obtained usingType.GetType