views:

77

answers:

3

Hi All, I am using c# & .NET 3.5. A vendor gives us an object that has properties of UserVarNbr1, UserVarData1, UserVarNbr2, UserVarData2,... UserVarNbrN, UserVarDataN. Not the way I would have coded it but nonetheless, this is what we have to work with. Another object in the application returns a collection of items used to represent these UserVariables.

The collection items have properties like this

public string VariableName
{
  get { return _VariableName; }
  set { _VariableName = value; }
}


public string VariableData
{
  get { return _VariableData; }
  set { _VariableData = value; }
}

I need to loop through the collection and create an instance of the vendor object and set the correct properties. UserVarNbrN, UserVarDataN needs to be put in the correct place. Note the collection returns a VariableName as a string "03", this needs to drive VendorObject properties UserVarNbr3, UserVarData3 **notice no "0" in the actual property name. How do I reference the correct property to get/set?

var o = new VendorObj();

I have something like this so far.

foreach (var item in userVars)
        {
            const string propPrefix = "UserVar";
            int varNum;
            var isNum = int.TryParse(item.VariableName, out varNum);

            if(isNum)
            {
                PropertyInfo pi;
                //this is where I am stuck
                // I need to set the corresponding properties on o
                // example if varNum == 38, how do I reference
                // o.(propPrefix+"Nbr"+varNum.ToString())
                // and
                // o.(propPrefix+"Data"+varNum.ToString())
                // so I may set them? 
            }
        }

Any help is appreciated. I am a rookie when it come to reflection.

Thanks, ~ck in San Diego

+2  A: 
VendorObj vndr = new VendorObj();

Console.WriteLine("\nInitial value of instance property: {0}", vndr.InstanceProperty);

PropertyInfo piInstance = typeof(VendorObj).GetProperty("InstanceProperty");

Object obj = piInstance.GetValue(vndr, null);

piInstance.SetValue(vndr, 37, null);

Console.WriteLine("Final value of instance property: {0}", vndr.InstanceProperty);
Gabriel McAdams
+1  A: 

Try this:

const string propPrefix = "UserVar";

VendorObj o = new VendorObj();
foreach (var item in userVars)
{
    int varNum = 0;
    if (Int32.TryParse(item.VariableName, out varNum))
    {
        string name = String.Format("{0}Nbr{1}", propPrefix, varNum);
        o.GetType().GetProperty(name).SetValue(o, "some value", null);
    }
}
Rubens Farias
All great answers. Thanks everyone.
Hcabnettek
A: 

Since you will be setting many properties on the one object, you are better off getting the PropertyDescriptorCollection

var o = new VendorObj();
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(o);
foreach (var item in userVars)
{
    const string propPrefix = "userVar";
    int varNum;
    if (int.TryParse(item.VariableName, out varNum))
    {
        PropertyDescriptor property = properties.Find(propPrefix + "Nbr" + varNum, true);
        property.SetValue(o, item.VariableData);
    }
}
DevDelivery