示例#1
0
        /// <summary>
        ///   Establishes the binding.
        /// </summary>
        public void Bind()
        {
            var metadataAttribute    = BindingMetadataAttribute.Get(RequiredPort.GetMethod());
            var delegateField        = metadataAttribute.DelegateField;
            var providedPortDelegate = ProvidedPort.CreateDelegate(delegateField.FieldType);

            delegateField.SetValue(RequiredPort.TargetObject, providedPortDelegate);
        }
示例#2
0
        /// <summary>
        ///   Initializes a new instance.
        /// </summary>
        /// <param name="requiredPort">The reference to the required port that should be bound.</param>
        /// <param name="providedPort">The reference to the provided port that should be bound.</param>
        public PortBinding(PortReference requiredPort, PortReference providedPort)
        {
            Requires.NotNull(requiredPort, nameof(requiredPort));
            Requires.NotNull(providedPort, nameof(providedPort));

            RequiredPort = requiredPort;
            ProvidedPort = providedPort;

            var metadataAttribute = BindingMetadataAttribute.Get(RequiredPort.GetMethod());

            metadataAttribute.BindingField.SetValue(requiredPort.TargetObject, this);
        }
示例#3
0
        /// <summary>
        ///   Binds all <see cref="PortBinding" /> instances found in the <paramref name="objects" />.
        /// </summary>
        /// <param name="objects">The objects that should be bound.</param>
        internal static void BindAll(IEnumerable <object> objects)
        {
            Requires.NotNull(objects, nameof(objects));

            // Set all default bindings so that we can be sure to get a helpful error message when
            // an unbound port is called
            foreach (var component in objects.OfType <Component>())
            {
                foreach (var requiredPort in component.GetRequiredPorts())
                {
                    var metadata = BindingMetadataAttribute.Get(requiredPort);
                    metadata.DefaultMethod.Invoke(component, new object[0]);
                }
            }

            // Set all bindings that were initialized at model construction time
            foreach (var binding in objects.OfType <PortBinding>())
            {
                binding.Bind();
            }
        }