title: Getting property values from a static class using reflection
body: <p>I have a class that is used for storing user data to a file. It works well, but can't really be placed into a PCL library easily. Outside of the PCL, it's all fine.</p>
  
  <p>The class looks like this</p>
  
  <pre><code>public static class UserData
  {
      public static object GetPropertyValue(this object data, string propertyName)
      {
          return data.GetType().GetProperties().Single(pi =&gt; pi.Name == propertyName).GetValue(data, null);
      }
  
      public static object SetPropertyValue&lt;T&gt;(this object data, string propertyName, T value)
      {
          data.GetType().GetProperties().Single(pi =&gt; pi.Name == propertyName).SetValue(data, value);
          return new object();
      }
  
      private static string pUserSettingsFile;
      private static UserSettings userSetting;
  
      public static bool AccountEnabled
      {
          get
          {
              return UserSettings.account_enabled;
          }
  
          set
          {
              UserSettings settings = UserSettings;
              settings.account_enabled = value;
              UserSettings = settings;
          }
      }
  
     public static UserSettings UserSettings
      {
          get
          {
              if (userSetting == null)
              {
                  if (File.Exists(UserSettingsFile))
                  {
                      userSetting = Serializer.XmlDeserializeObject&lt;UserSettings&gt;(UserSettingsFile);
                  }
                  else
                  {
                      userSetting = new UserSettings();
                      Serializer.XmlSerializeObject(userSetting, UserSettingsFile);
                  }
              }
  
              return userSetting;
          }
  
          set
          {
              if (value == null)
              {
                  throw new ArgumentNullException("value is null!");
              }
  
              userSetting = value;
              if (File.Exists(UserSettingsFile))
              {
                  File.Delete(UserSettingsFile);
              }
  
              Serializer.XmlSerializeObject(userSetting, UserSettingsFile);
          }
      }
  
      public static string UserSettingsFile
      {
          get
          {
              if (string.IsNullOrEmpty(pUserSettingsFile))
              {
                  pUserSettingsFile = Path.Combine(GroupShootDroid.Singleton.ContentDirectory, "UserSettings.xml");
              }
  
              return pUserSettingsFile;
          }
      }
  
      #endregion
  }
  
  public class UserSettings
  {
      public bool account_enabled { get; set; }
  
      public string address { get; set; }
  
      public string country { get; set; }
  }
  </code></pre>
  
  <p>It's not rocket science, but does what I need it to do.</p>
  
  <p>What I'm trying to do is use the Get/SetPropertyValue methods to return or set any of the properties within the class.</p>
  
  <p>Currently, to access the Get/SetPropertyValue methods I'm using this</p>
  
  <pre><code>    public string GetStringValue(string valToGet)
      {
          string rv = (string)UserData.GetPropertyValue(valToGet);
          return rv;
      }
  
      public void SetStringValue(string name, string val)
      {
          UserData.SetPropertyValue(name, val);
      }
  </code></pre>
  
  <p>On compiling though, the <code>GetPropertyValue</code> method is giving an error that <code>No overload for method GetPropertyValue takes 1 argument</code> with the <code>SetPropertyValue</code> complaining that there isn't an overload that takes 2</p>
  
  <p>I'm not sure that the code I'm using will do what I need it to do (from what I've read on here it should be), but I'm more perplexed as to why the errors are showing.</p>
  
  <p>Is there a better way to do what I'm trying to do? The application is a Xam.Forms app, so the PCL accesses the class through an interface using injection.</p>