lundi 28 septembre 2015

Looping through Generic Class's collection property

Although many questions have been posted, none seem to help me on my issue.

I've started a new Generics / Reflection adventure and I'm just trying to get my head around the syntax and concepts.

I have a Generic class with X amount of properties and one being a collection, all is working fine but I'm having problems extracting the values from the collection props by property name.

foreach (var property in typeof(T).GetProperties())
{
    if (property.Name == "Props")
    {
        foreach (var item in (IEnumerable)property.GetValue(type, null))
        {
            var propertyName = "";
            var newValue = "";
            var oldValue = "";

            sbDescription.AppendLine(strDescriptionVals
               .Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "" + ", "));

            sbAllNotes.AppendLine(strAllNotes
               .Replace("{0}", (item.ToString() == "PropertyName") ? item.ToString() : "")
               .Replace("{1}", (item.ToString() == "NewValue") ? item.ToString() : "")
               .Replace("{2}", (item.ToString() == "OldValue") ? item.ToString() : ""));
        }
    }
}

As you can see I've pinpointed the property Props and now I want to loop through that and pull values by property name.

item.ToString() just prints the namespace of the class property and not the value

Hoping you kind folk can point me in the right direction?





Aucun commentaire:

Enregistrer un commentaire