mercredi 3 juin 2015

C# - How to get value of unknown properties (part solved already with reflection)

I have an existing c# application to modify and need to loop through an object with unknown properties and have half solved the problem with reflection.

I'm trying to populate a dictionary with the property name and the property value. The code is below and I've given a description of what I need in between the ***s

This is an MVC5 project

    private Dictionary<string, string> StoreUserDetails ()
    {      
      var userDetails = new Dictionary<string, string>();

      foreach (var userItem in UserItems)
      {
        var theType = userItem.GetType();
        var theProperties = theType.GetProperties();

        foreach (var property in theProperties)
        {
          userDetails.Add(property.Name, ***value of userItem property with this property name***);
        }
      }      
      return userDetails;
    }

Many thanks in advance for your help.





Aucun commentaire:

Enregistrer un commentaire