vendredi 14 septembre 2018

Using a variable to access object member value

I'm trying to access member values of the following class:

public class EditorialDateFormat
{
       public string en; <------ TRYING TO GET ITS VALUE
       public string fr; <------ OR THIS VALUE
       public string Default;<-- OR THIS ONE
 }
public class Params
{
      public string Template;
      public string MainTagID;
      public string[] NavigationFilters;
      public EditorialDateFormat EditorialDateFormat;
}
public class Site
{
      public string Name;
      public string CreationFolder;
      public Params Params;
      public string[] Feed;
      public string Endpoint;
      public string[] TargetDatabases;
}

I could do it easily like : site.Params.EditorialDateFormat.en(for example) but "en" string is actually saved in a variable.

I tried the following code :

object c = GetPropValue(site.Params.EditorialDateFormat, MyVariable); // Myvariable contains "en"
public static object GetPropValue(object src, string propName)
{
    return src.GetType().GetMember(propName);
}

But it returns me a memberInfo object without the member value I would appreciate some help! Thanks in advance





Aucun commentaire:

Enregistrer un commentaire