How to iterate over numerically named object properties
So I have a horribly designed class that I can't change that has properties like this:
object.Color1
object.Color2
object.Color3
etc...
How can I iterate through those with a for loop. In other words, something like this:
for (int i = 0; i <= 40; i++)
{
string PropertyName = "Color" + i;
if (object.PropertyName != "")
{
// do something
}
}
Obviously this code wouldn't work but it gives you an idea of what I'm after. I have to do some processing on each property and I don't want to repeat my code 40 times. :) A loop would be perfect, I'm just not sure how to create the name of the property on the fly.
EDIT: Ok so I've tried the following code:
for (int i = 1; i <= 20; i++ )
{
var type = pendingProduct.GetType();
var colorProperty = type.GetProperty("Color" + i);
开发者_JAVA百科 string colorValue = colorProperty.GetValue(type, null).ToString();
var colorSkuProperty = type.GetProperty("Color" + i + "SKU");
string colorSkuValue = colorSkuProperty.GetValue(type, null).ToString();
if (String.IsNullOrEmpty(colorValue)) continue;
ProductColor color = new ProductColor {Color = colorValue, ProductSizes = productSizes};
if (!String.IsNullOrEmpty(colorSkuValue)) color.SKU = colorSkuValue;
}
I'm getting an error "Object does not match target type" on this line:
string colorValue = colorProperty.GetValue(type, null).ToString();
Am I doing something wrong here?
You're looking for reflection:
PropertyInfo property = typeof(SomeType).GetProperty("Color" + i);
string value = (string)property.GetValue(obj, null);
Note that this will be slow.
If you do it many times, you can make it faster by caching Delegate.CreateDelegate(..., property.GetGetMethod())
in an array.
You can use reflection for that:
var type = myObject.GetType();
var property = type.GetProperty("Color1");
var value = property.GetValue(myObject, null));
This is how i do to help finding.
$abc = array(
'a' => 'world',
'b' => 'good',
);
$object = (object) $abc;
foreach($object as $k)
{
var_dump($object);
}
You can use reflection to get the property by name. In your example you can use:
var pi = obj.GetType().GetProperty(PropertyName);
var val = pi.GetValue(obj,null);
In order to obtain the value of the property which name is PropertyName. You should check for pi != null because if a requested property does not exists null is returned. If the function you are writing is time critical, you should anyway pay attention that reflection has some performance drawbacks.
Have a look at InvokeMethod...
MSDN
Example at codeproject
You could try something along the lines of...
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo p in properties)
{
// Check property is the one you want
// and carry out your code...
}
Also, If you have access to the code I.E. you can change the internals but want to leave the public API intact you could just add the information to a private collection when the object is constructed. The collection could then be exposed by a public property for you to iterate over. A bit hacky but an alternative to using reflection.
精彩评论