예제 #1
0
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            var attr = context.Parameter.GetCustomAttribute <TAttribute>();

            if (attr == null)
            {
                return(null);
            }

            if (_validator != null)
            {
                // Expected this will throw on errors.
                Type parameterType    = context.Parameter.ParameterType;
                var  cloner           = new AttributeCloner <TAttribute>(attr, context.BindingDataContract, _nameResolver);
                var  attrNameResolved = cloner.GetNameResolvedAttribute();
                _validator(attrNameResolved, parameterType);
            }

            foreach (IBindingProvider provider in _providers)
            {
                IBinding binding = await provider.TryCreateAsync(context);

                if (binding != null)
                {
                    return(binding);
                }
            }

            // Nobody claimed it.
            string       resourceName = typeof(TAttribute).Name;
            const string Suffix       = "Attribute";

            if (resourceName.EndsWith(Suffix))
            {
                resourceName = resourceName.Substring(0, resourceName.Length - Suffix.Length);
            }

            StringBuilder exceptionMessage = new StringBuilder($"Can't bind {resourceName} to type '{context.Parameter.ParameterType}'.");

            if (context.BindingErrors.Count > 0)
            {
                exceptionMessage.AppendLine().AppendLine("Possible causes:");

                var index = 0;
                foreach (var error in context.BindingErrors)
                {
                    exceptionMessage.AppendLine($"{++index}) {error}");
                }
            }

            throw new InvalidOperationException(exceptionMessage.ToString());
        }
        // Called once per method definition. Very static.
        public Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo parameter = context.Parameter;
            TAttribute    attribute = parameter.GetCustomAttribute <TAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <IBinding>(null));
            }

            // Now we can instantiate against the user's type.
            // throws if can't infer the type.
            Type typeMessage = BindingFactoryHelpers.GetAsyncCollectorCoreType(parameter.ParameterType);

            if (typeMessage == null)
            {
                // incompatible type. Skip.
                return(Task.FromResult <IBinding>(null));
            }
            // Apply filter
            var  cloner           = new AttributeCloner <TAttribute>(attribute, context.BindingDataContract, _nameResolver);
            var  attrNameResolved = cloner.GetNameResolvedAttribute();
            bool canUse           = _filter(attrNameResolved, typeMessage);

            if (!canUse)
            {
                return(Task.FromResult <IBinding>(null));
            }

            var wrapper = WrapperBase.New(
                typeMessage, _builder, _nameResolver, parameter, context);

            IBinding binding = wrapper.CreateBinding();

            return(Task.FromResult(binding));
        }
        public Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            Type parametertype = context.Parameter.ParameterType;

            var attr = context.Parameter.GetCustomAttribute <TAttribute>();

            var cloner           = new AttributeCloner <TAttribute>(attr, context.BindingDataContract, _nameResolver);
            var attrNameResolved = cloner.GetNameResolvedAttribute();

            // This may do validation and throw too.
            bool canBind = _predicate(attrNameResolved, parametertype);

            if (!canBind)
            {
                return(Task.FromResult <IBinding>(null));
            }
            return(_inner.TryCreateAsync(context));
        }
예제 #4
0
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            var attr = context.Parameter.GetCustomAttribute <TAttribute>();

            if (attr == null)
            {
                return(null);
            }

            if (_validator != null)
            {
                // Expected this will throw on errors.
                Type parameterType    = context.Parameter.ParameterType;
                var  cloner           = new AttributeCloner <TAttribute>(attr, context.BindingDataContract, _nameResolver);
                var  attrNameResolved = cloner.GetNameResolvedAttribute();
                _validator(attrNameResolved, parameterType);
            }

            foreach (IBindingProvider provider in _providers)
            {
                IBinding binding = await provider.TryCreateAsync(context);

                if (binding != null)
                {
                    return(binding);
                }
            }

            // Nobody claimed it.
            string       resourceName = typeof(TAttribute).Name;
            const string Suffix       = "Attribute";

            if (resourceName.EndsWith(Suffix))
            {
                resourceName = resourceName.Substring(0, resourceName.Length - Suffix.Length);
            }
            throw new InvalidOperationException("Can't bind " + resourceName + " to type '" + context.Parameter.ParameterType + "'.");
        }
예제 #5
0
        public async Task <IBinding> TryCreateAsync(BindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var binding = await _inner.TryCreateAsync(context);

            if (binding != null)
            {
                // Only run the validator if the inner binding claims it.
                // Expected this will throw on errors.
                Type parameterType = context.Parameter.ParameterType;
                var  attr          = context.Parameter.GetCustomAttribute <TAttribute>();

                var cloner           = new AttributeCloner <TAttribute>(attr, context.BindingDataContract, _configuration, _nameResolver);
                var attrNameResolved = cloner.GetNameResolvedAttribute();
                _validator(attrNameResolved, parameterType);
            }

            return(binding);
        }