Exemple #1
0
        /// <summary>
        /// Add a binding rule for the given attribute
        /// </summary>
        /// <typeparam name="TAttribute"></typeparam>
        /// <returns></returns>
        public FluentBindingRule <TAttribute> AddBindingRule <TAttribute>() where TAttribute : Attribute
        {
            var bindingAttrs = typeof(TAttribute).GetCustomAttributes(typeof(BindingAttribute), false);

            if (!bindingAttrs.Any())
            {
                throw new InvalidOperationException($"Can't add a binding rule for '{typeof(TAttribute).Name}' since it is missing the a {typeof(BindingAttribute).Name}");
            }

            var isTrigger = typeof(TAttribute).Name.EndsWith("TriggerAttribute", StringComparison.OrdinalIgnoreCase);

            if (!isTrigger && ((BindingAttribute)bindingAttrs.First()).TriggerHandlesReturnValue)
            {
                throw new InvalidOperationException($"Only declare {nameof(BindingAttribute.TriggerHandlesReturnValue)} property true for trigger bindings.");
            }

            if (!_existingRules.Add(typeof(TAttribute)))
            {
                throw new InvalidOperationException($"Only call AddBindingRule once per attribute type.");
            }

            var fluent = new FluentBindingRule <TAttribute>(this.Config);

            _updates.Add(fluent.ApplyRules);
            return(fluent);
        }
Exemple #2
0
        /// <summary>
        /// Add a binding rule for the given attribute
        /// </summary>
        /// <typeparam name="TAttribute"></typeparam>
        /// <returns></returns>
        public FluentBindingRule <TAttribute> AddBindingRule <TAttribute>() where TAttribute : Attribute
        {
            bool hasBindingAttr = typeof(TAttribute).GetCustomAttributes(typeof(BindingAttribute), false).Length > 0;

            if (!hasBindingAttr)
            {
                throw new InvalidOperationException($"Can't add a binding rule for '{typeof(TAttribute).Name}' since it is missing the a {typeof(BindingAttribute).Name}");
            }

            if (!_existingRules.Add(typeof(TAttribute)))
            {
                throw new InvalidOperationException($"Only call AddBindingRule once per attribute type.");
            }

            var fluent = new FluentBindingRule <TAttribute>(this.Config);

            _updates.Add(fluent.ApplyRules);
            return(fluent);
        }
        // Ensure that multiple attempts bind to the same attribute, they get the same rule object.
        private FluentBindingRule <TAttribute> GetOrCreate <TAttribute>()
            where TAttribute : Attribute
        {
            FluentBindingRule <TAttribute> rule;

            if (!this._rules.TryGetValue(typeof(TAttribute), out object temp))
            {
                // Create and register
                rule = new FluentBindingRule <TAttribute>(_nameResolver, _converterManager, _extensionRegistry);
                this._rules[typeof(TAttribute)] = rule;

                _updates.Add(rule.ApplyRules);
            }
            else
            {
                // Return existing.
                rule = (FluentBindingRule <TAttribute>)temp;
            }
            return(rule);
        }
Exemple #4
0
        // Ensure that multiple attempts bind to the same attribute, they get the same rule object.
        private FluentBindingRule <TAttribute> GetOrCreate <TAttribute>()
            where TAttribute : Attribute
        {
            FluentBindingRule <TAttribute> rule;
            object temp;

            if (!this._rules.TryGetValue(typeof(TAttribute), out temp))
            {
                // Create and register
                rule = new FluentBindingRule <TAttribute>(this.Config);
                this._rules[typeof(TAttribute)] = rule;

                _updates.Add(rule.ApplyRules);
            }
            else
            {
                // Return existing.
                rule = (FluentBindingRule <TAttribute>)temp;
            }
            return(rule);
        }