using enums in refleciton

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;
}
 
D

Dave Sexton

Hi,

You should use a TypeConverter for all of the conversions when one is
available that can perform the conversion:

TypeConverter converter = TypeDescriptor.GetConverter(t3.PropertyType);

if (converter != null && converter.CanConvertFrom(typeof(string)))
{
t3.SetValue(inpObject,
converter.ConvertFromString(newValues), null);
}

Enum returns an EnumConverter that can convert to/from strings. All of the
framework primitive types have specific converters as well, so your switch
statement won't be necessary.
 
B

bshumsky06

Awesome, thanks! I am not sure if I am phrasing this correctly but is
there any easy way for me to loop through all the objects starting with
the base object to get to the one that has the property I want to set.
For exampe say I have
baseObjectPerson.arm.hand.indexfinger.nail.length=10 and I parse the
left hand side to
baseObjectPerson.
arm
hand
etc. how do I recursively act on each object to get me to nail.length?


Thanks,
Bob

Dave said:
Hi,

You should use a TypeConverter for all of the conversions when one is
available that can perform the conversion:

TypeConverter converter = TypeDescriptor.GetConverter(t3.PropertyType);

if (converter != null && converter.CanConvertFrom(typeof(string)))
{
t3.SetValue(inpObject,
converter.ConvertFromString(newValues), null);
}

Enum returns an EnumConverter that can convert to/from strings. All of the
framework primitive types have specific converters as well, so your switch
statement won't be necessary.

--
Dave Sexton
http://davesexton.com/blog

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;
}
 
D

Dave Sexton

Hi Bob,

Your request doesn't make much sense though since if you know the type of
baseObjectPerson then don't need reflection, just cast it instead. If you
don't know what the type of the object will be at runtime, but you're sure
that it has a property called Arm, of some type that has a property called
Hand, then you'll probably know what the class or interface of Arm, Hand,
Finger, Nail, etc. are so you won't need reflection for that either and can
just perform a simple cast:

PersonWithArm person = baseObjectPerson as PersonWithArm;

if (person != null)
return person.Arm.Hand.IndexFinger.Nail.Length;
else
return 0;

If you don't know what the type of object baseObjectPerson will be at
runtime and you don't know what classes or interfaces that it might be then
you should probably choose a better design. Regardless, you can get all of
the properties for baseObjectPerson by calling the
baseObjectPerson.GetType().GetProperties method. From the returned
PropertyInfo array choose the one that you need depending upon your
requirements. Although, it doesn't make sense to expect a property named
Arm on some arbitrary object since the odds of it actually existing are
quite bad :)

--
Dave Sexton
http://davesexton.com/blog

Awesome, thanks! I am not sure if I am phrasing this correctly but is
there any easy way for me to loop through all the objects starting with
the base object to get to the one that has the property I want to set.
For exampe say I have
baseObjectPerson.arm.hand.indexfinger.nail.length=10 and I parse the
left hand side to
baseObjectPerson.
arm
hand
etc. how do I recursively act on each object to get me to nail.length?


Thanks,
Bob

Dave said:
Hi,

You should use a TypeConverter for all of the conversions when one is
available that can perform the conversion:

TypeConverter converter = TypeDescriptor.GetConverter(t3.PropertyType);

if (converter != null && converter.CanConvertFrom(typeof(string)))
{
t3.SetValue(inpObject,
converter.ConvertFromString(newValues), null);
}

Enum returns an EnumConverter that can convert to/from strings. All of
the
framework primitive types have specific converters as well, so your
switch
statement won't be necessary.

--
Dave Sexton
http://davesexton.com/blog

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;
}

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top