B
bshumsky06
Hi,
I am trying to parse some text into c# commands using reflection. I am
new to this so there may be a simpler way of doing things. Anyhow, the
below code works fine except for enums. PropHere is an array
containing the string name of the property I want to set and newValues
is an array containing the string representation of the value I want to
set the property to. I use the valueType array to cast the string
values to the appropriate type. Is there a way that I can use
Enum.parse or some other approach to cast a string to the appropriate
enum value, keeping in mind that the valueType array contains strings,
not actual enum objects.
PropertyInfo t3 = inpObject.GetType().GetProperty(propHere);
object[] nullArray = new object[0];
switch (valueType)
{
case "float":
t3.SetValue(inpObject, float.Parse(newValues),
nullArray);
break;
case "String":
t3.SetValue(inpObject, newValues, nullArray);
break;
case "int":
t3.SetValue(inpObject, int.Parse(newValues),
nullArray);
break;
case "bool":
t3.SetValue(inpObject, bool.Parse(newValues),
nullArray);
break;
case "double":
t3.SetValue(inpObject, double.Parse(newValues),
nullArray);
break;
}
I am trying to parse some text into c# commands using reflection. I am
new to this so there may be a simpler way of doing things. Anyhow, the
below code works fine except for enums. PropHere is an array
containing the string name of the property I want to set and newValues
is an array containing the string representation of the value I want to
set the property to. I use the valueType array to cast the string
values to the appropriate type. Is there a way that I can use
Enum.parse or some other approach to cast a string to the appropriate
enum value, keeping in mind that the valueType array contains strings,
not actual enum objects.
PropertyInfo t3 = inpObject.GetType().GetProperty(propHere);
object[] nullArray = new object[0];
switch (valueType)
{
case "float":
t3.SetValue(inpObject, float.Parse(newValues),
nullArray);
break;
case "String":
t3.SetValue(inpObject, newValues, nullArray);
break;
case "int":
t3.SetValue(inpObject, int.Parse(newValues),
nullArray);
break;
case "bool":
t3.SetValue(inpObject, bool.Parse(newValues),
nullArray);
break;
case "double":
t3.SetValue(inpObject, double.Parse(newValues),
nullArray);
break;
}