Пример #1
0
 private static void FillImpl(SettingsFetcherMethod method, object instanceOrType)
 {
     if (instanceOrType is TypeInfo info)
     {
         FillImpl(method, info.GetCustomAttribute <System.Configuration.SettingsGroupNameAttribute>()?.GroupName, info);
     }
     else if (instanceOrType is Type type)
     {
         FillImpl(method, type.GetCustomAttribute <System.Configuration.SettingsGroupNameAttribute>()?.GroupName, type.GetTypeInfo());
     }
     else if (instanceOrType != null)
     {
         FillImpl(method, instanceOrType.GetType().GetTypeInfo().GetCustomAttribute <System.Configuration.SettingsGroupNameAttribute>()?.GroupName, instanceOrType);
     }
 }
Пример #2
0
        private static void FillImpl(SettingsFetcherMethod method, string groupName, object instance)
        {
            if (instance == null)
            {
                return;
            }

            if (method == null)
            {
                var mhd = _method;
                if (mhd == null || mhd.Getter != Getter || mhd.Converter != Converter || mhd.JoinName != JoinName || mhd.ThrowError != ThrowError)
                {
                    _method = mhd = new SettingsFetcherMethod(Getter, Converter, JoinName, ThrowError);
                }
                method = mhd;
            }
            TypeInfo type;

            if (instance is TypeInfo info)
            {
                type     = info;
                instance = null;
            }
            else if (instance is Type t)
            {
                type     = t.GetTypeInfo();
                instance = null;
            }
            else
            {
                type = instance.GetType().GetTypeInfo();
            }

            var isStatic = instance == null;

            foreach (var prop in type.DeclaredProperties)
            {
                if (prop.CanWrite && prop.SetMethod.IsStatic == isStatic)
                {
                    var value = method.GetSetting(groupName, prop.Name, prop.PropertyType);
                    if (value != null)
                    {
                        prop.SetValue(instance, value);
                    }
                }
            }
        }