Search code examples
c#.netpropertiesdefault-valuec#-6.0

How to get default compile-time value of Auto-Implemented property C# 6.0 after it changed?


Shortly, The new C# 6.0 Auto-Implemented Property allows us to make this

    public static bool IsSoundEffects { get; set; } = true;   // C# 6.0 allows this

Now in somewhere, I changed the property IsSoundEffects = false, So accessing it will be false.

hmm, So how to get the actual real default compile-time auto-implemented property value.

Something Like :
Type.GetPropertyDefaultValue(IsSoundEffects); // A real compile-time one = true

OR

default(IsSoundEffects)   // idk, something like that

Why I need that?

because I filling the properties from the database. and restore it if user need to restore the default values. for example settings.

Looks strange? I searched enough but all examples about the auto-implemented feature did not restore the default value.

Edited

The best approaches provided by

xiangbin.pang answer for reflection way [Short-One]

Christopher answers for constants as default values.


Solution

    1. For instance property, just new an instance then get the default property value is the easiest way.
    2. For static property, the default value can be preserved in the static constructor.
        public static class MyClass
        {
            public static int MyProp1 { get; set; } = 100;
            public static bool MyProp2 { get; set; } = false;
    
            private static Dictionary<string, object> defaultValues;
    
            static MyClass()
            {
                defaultValues = new Dictionary<string, object>();
    
                foreach(var prop in typeof(MyClass).GetProperties(BindingFlags.Static| BindingFlags.Public | BindingFlags.NonPublic))
                {
                    defaultValues[prop.Name] = prop.GetValue(null);
                }
            }
    
            public static (T,bool) GetDefault<T>(string propName)
            {
                if(defaultValues.TryGetValue(propName, out object value))
                {
                    return ((T)(value), true);
                }
                return (default, false);
            }
        }
    
        //test codes
        static void Main(string[] args)
        {
    
            MyClass.MyProp1 = 1000;
            MyClass.MyProp2 = true;
    
            var defaultValueOrProp1 = MyClass.GetDefault<int>("MyProp1");
            if(defaultValueOrProp1.Item2)
            {
                Console.WriteLine(defaultValueOrProp1.Item1);//100
            }
    
            var defaultValueOrProp2 = MyClass.GetDefault<bool>("MyProp2");
            if (defaultValueOrProp2.Item2)
            {
                Console.WriteLine(defaultValueOrProp2.Item1);//false
            }
        }
    



    Following Line added by question author:

    For setting property with default value

    private static void ResetPropertyValue(string PropertyName)
    { 
        typeof(Options).GetProperty(PropertyName).SetValue(null, 
        defaultValues[PropertyName]);
    }