简体   繁体   中英

Getting property values from a static class using reflection

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.

The class looks like this

public static class UserData
{
    public static object GetPropertyValue(this object data, string propertyName)
    {
        return data.GetType().GetProperties().Single(pi => pi.Name == propertyName).GetValue(data, null);
    }

    public static object SetPropertyValue<T>(this object data, string propertyName, T value)
    {
        data.GetType().GetProperties().Single(pi => 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<UserSettings>(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; }
}

It's not rocket science, but does what I need it to do.

What I'm trying to do is use the Get/SetPropertyValue methods to return or set any of the properties within the class.

Currently, to access the Get/SetPropertyValue methods I'm using this

    public string GetStringValue(string valToGet)
    {
        string rv = (string)UserData.GetPropertyValue(valToGet);
        return rv;
    }

    public void SetStringValue(string name, string val)
    {
        UserData.SetPropertyValue(name, val);
    }

On compiling though, the GetPropertyValue method is giving an error that No overload for method GetPropertyValue takes 1 argument with the SetPropertyValue complaining that there isn't an overload that takes 2

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.

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.

You are defining extension method, you need an instance of the class to call them:

var o = new Object();
string rv = (string)o.GetPropertyValue(valToGet);
// or, but no sure
string rv = (string)UserData.GetPropertyValue(o, valToGet);

or more probably in your case:

public string GetStringValue(string valToGet)
{
    string rv = (string)this.GetPropertyValue(this, valToGet);        
    //or
    //string rv = (string)UserData.GetPropertyValue(this, valToGet);        
    return rv;
}

I think you're getting confused between the UserData class and the object class. Your extension methods extend object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM