/// <summary> /// Binds the specified collection of attributes. /// </summary> /// <typeparam name="TValue">The type to bind to.</typeparam> /// <param name="attributes">The collection of attributes to bind. The first attribute in the /// collection should be the primary attribute.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns></returns> public virtual async Task <TValue> BindAsync <TValue>(Attribute[] attributes, CancellationToken cancellationToken = default(CancellationToken)) { var attribute = attributes.First(); var additionalAttributes = attributes.Skip(1).ToArray(); IBinding binding = await _bindingSource.BindAsync <TValue>(attribute, additionalAttributes, cancellationToken); if (binding == null) { throw new InvalidOperationException("No binding found for attribute '" + attribute.GetType() + "'."); } // Create a clone of the binding context, so any binding data that was added // will be applied to the binding var ambientBindingContext = new AmbientBindingContext(_bindingSource.AmbientBindingContext.FunctionContext, _bindingData); var bindingContext = new BindingContext(ambientBindingContext, cancellationToken); IValueProvider provider = await binding.BindAsync(bindingContext); if (provider == null) { return(default(TValue)); } Debug.Assert(provider.Type == typeof(TValue)); ParameterDescriptor parameterDesciptor = binding.ToParameterDescriptor(); parameterDesciptor.Name = null; // Remove the dummy name "?" used for runtime binding. // Add even if watchable is null to show parameter descriptor in status. string value = provider.ToInvokeString(); IWatchable watchable = provider as IWatchable; _watcher.Add(parameterDesciptor, value, watchable); IValueBinder binder = provider as IValueBinder; if (binder != null) { _binders.Add(binder); } IDisposable disposableProvider = provider as IDisposable; if (disposableProvider != null) { _disposable.Add(disposableProvider); } object result = await provider.GetValueAsync(); return((TValue)result); }
public AttributeBindingSource(IBindingProvider bindingProvider, AmbientBindingContext context) { if (bindingProvider == null) { throw new ArgumentNullException("bindingProvider"); } if (context == null) { throw new ArgumentNullException("context"); } _bindingProvider = bindingProvider; _context = context; }
public AttributeBindingSource(MemberInfo memberInfo, IBindingProvider bindingProvider, AmbientBindingContext context) { if (memberInfo == null) { throw new ArgumentNullException("memberInfo"); } if (bindingProvider == null) { throw new ArgumentNullException("bindingProvider"); } if (context == null) { throw new ArgumentNullException("context"); } _memberInfo = memberInfo; _bindingProvider = bindingProvider; _context = context; }