Example #1
0
        protected void PostProceed(IInvocation invocation)
        {
            var setFlag = "set_";
            var getFlag = "get_";

            var isSet = invocation.Method.Name.StartsWith(setFlag);
            var isGet = invocation.Method.Name.StartsWith(getFlag);

            //非属性方法不处理
            if (!isSet && !isGet)
            {
                return;
            }

            var pname = invocation.Method.Name.Replace(setFlag, "")
                        .Replace(getFlag, "");
            var settingName = AutoSettingsUtils.CreateSettingName(invocation.TargetType, pname);

            //配置 设置
            if (isSet)
            {
                var setting = this._settingDefinitionManager.GetSettingDefinition(settingName);
                this.ChangeSettingValue(setting, invocation.Arguments[0]?.ToString());
            }
            //配置 获取
            else
            {
                var val = this._settingManager.GetSettingValue(settingName);
                invocation.ReturnValue = ConvertHelper.ChangeType(val, invocation.Method.ReturnType);
            }
        }
        public override IEnumerable <SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {
            var settings = new List <SettingDefinition>();

            foreach (Assembly assembly in _assemblyFinder.GetAllAssemblies())
            {
                var types = assembly
                            .GetTypes()
                            .Where(t => t.IsClass && typeof(ISettings).IsAssignableFrom(t));
                foreach (var type in types)
                {
                    var scopes = SettingScopes.All;
                    foreach (var p in type.GetProperties())
                    {
                        var key = AutoSettingsUtils.CreateSettingName(type, p.Name);
                        var isVisibleToClients = false;
                        var defaultValue       = AutoSettingsUtils.GetDefaultValue(p.PropertyType);
                        var attr = p.GetCustomAttribute <AutoSettingDefinitionAttribute>();
                        if (attr != null)
                        {
                            scopes             = attr.Scopes;
                            defaultValue       = attr.DefaultValue;
                            isVisibleToClients = attr.IsVisibleToClients;
                        }
                        settings.Add(new SettingDefinition(
                                         name: key,
                                         defaultValue: defaultValue?.ToString(),
                                         scopes: scopes,
                                         isVisibleToClients: isVisibleToClients
                                         ));
                    }
                }
            }

            //int io= types.Count();


            return(settings);
        }