예제 #1
0
        public async Task <bool> Bind(object instance, PropertyInfo propertyInfo, IBindingContext bindingContext)
        {
            if (!await _bindingSourceCollection.ContainsKey(bindingContext.GetKey(propertyInfo.Name), bindingContext.Environment).ConfigureAwait(false))
            {
                return(false);
            }

            var conversionResult = _valueConverterCollection.Convert(propertyInfo.PropertyType, await _bindingSourceCollection.Get(bindingContext.GetKey(propertyInfo.Name), bindingContext.Environment).ConfigureAwait(false), bindingContext);

            if (!conversionResult.Success)
            {
                return(false);
            }

            propertyInfo.SetValue(instance, conversionResult.Instance, new object[0]);

            return(true);
        }
예제 #2
0
        public async Task <BindingResult> Bind(Type type, IBindingContext bindingContext)
        {
            if (_valueConverterCollection.CanConvert(type, bindingContext))
            {
                return(_valueConverterCollection.Convert(type, await _bindingSourceCollection.Get(bindingContext.GetPrefix(), bindingContext.Environment).ConfigureAwait(false), bindingContext));
            }

            var binder = GetMatchingBinders(type).FirstOrDefault();

            if (binder == null)
            {
                bindingContext.Environment.Log("Failed to find a matching modelbinder for type: {0}", LogLevel.Info, type);
                return(new BindingResult(null, false));
            }

            bindingContext.Environment.Log("Going to bind type: {0} using {1}.", LogLevel.Debug, type, binder.GetType().Name);

            var result = await binder.Bind(type, bindingContext).ConfigureAwait(false);

            bindingContext.Environment.Log("Finished binding type: {0} using {1}. Result: Success = {2}, Instance = {3}.", LogLevel.Debug, type, binder.GetType().Name, result.Success, result.Instance?.ToString() ?? "null");

            return(result);
        }