2

So I need to access a class property's value dynamically at runtime but I can't figure out how to do this...any suggestions? Thanks!

//Works
int Order = OrdersEntity.ord_num;

//I would love for this to work.. it obviously does not.
string field_name = "ord_num";
int Order = OrdersEntity.(field_name);

OK so here is what I have so far with reflection, which balks unless the collection item it is looping through is a string:

void RefreshGrid(EntityCollection<UOffOrdersStgEcommerceEntity> collection)
        {
            List<string> col_list = new List<string>();

            foreach (UOffOrdersStgEcommerceEntity rec in collection)
            {
                foreach (System.Collections.Generic.KeyValuePair<string, Dictionary<string, string>> field in UOffOrdersStgEcommerceEntity.FieldsCustomProperties)
                {

                        if (!string.IsNullOrEmpty((string)rec.GetType().GetProperty(field.Key).GetValue(rec, null)))
                        {
                            if (!col_list.Contains<string>((string)rec.GetType().GetProperty(field.Key).GetValue(rec, null))) 
                                col_list.Add((string)rec.GetType().GetProperty(field.Key).GetValue(rec,null));
                        }

                }

                foreach (string ColName in col_list)
                {
                    grdOrders.Columns.Add(new DataGridTextColumn
                    {
                        Header = ColName,
                        Binding = new Binding(ColName)
                    });
                }               
            }

            grdOrders.ItemsSource = collection;
        }
diamondracer
  • 95
  • 1
  • 8

2 Answers2

6

If you want to do this, you have to use reflection:

int result = (int)OrdersEntity.GetType()
                              .GetProperty("ord_num")
                              .GetValue(OrdersEntity, null);
svick
  • 236,525
  • 50
  • 385
  • 514
eouw0o83hf
  • 9,438
  • 5
  • 53
  • 75
  • so I basically have a collection of properties, which may be strings, decimals, dates, etc & I basically want to dynamically add grid columns in a WPF app if the values are not null. Here's what I have so far, which works if all of the collection objects are strings, but they are not. The collection object is an llblgen collection btw.. code on my next reply. – diamondracer Mar 06 '12 at 19:06
0

It might not be exactly what you want to do, but try changing

if (!string.IsNullOrEmpty((string)rec.GetType().GetProperty(field.Key).GetValue(rec, null)))
{
    if (!col_list.Contains<string((string)rec.GetType().GetProperty(field.Key).GetValue(rec, null))) 
    col_list.Add((string)rec.GetType().GetProperty(field.Key).GetValue(rec,null));
}

to (with some refactoring)

string columnValue = rec.GetType().GetProperty(field.Key).GetValue(rec, null).ToString();
if (!string.IsNullOrEmpty(columnValue))
{
    if (!col_list.Contains(columnValue)) 
        col_list.Add(columnValue);
}

GetValue() returns an Object, so ToString() is the only way to reliably get a string in this case.

Steve Czetty
  • 6,147
  • 9
  • 39
  • 48