コード例 #1
0
        public void UsesDelegatePassedIn()
        {
            var finder = new DelegatePropertySelector((propInfo, instance) =>
            {
                return(propInfo.GetCustomAttributes <InjectPropertyAttribute>().Any());
            });

            foreach (var propInfo in typeof(HasProperties).GetProperties())
            {
                var expected = propInfo.GetCustomAttributes <InjectPropertyAttribute>().Any();
                Assert.Equal(expected, finder.InjectProperty(propInfo, null));
            }
        }
コード例 #2
0
        public void UsesDelegatePassedIn()
        {
            var finder = new DelegatePropertySelector((propInfo, instance) =>
            {
                return propInfo.GetCustomAttributes<InjectPropertyAttribute>().Any();
            });

            foreach (var propInfo in typeof(HasProperties).GetProperties())
            {
                var expected = propInfo.GetCustomAttributes<InjectPropertyAttribute>().Any();
                Assert.Equal(expected, finder.InjectProperty(propInfo, null));
            }
        }
コード例 #3
0
        private static ContainerBuilder GetConfiguredBuilder()
        {
            var builder = new ContainerBuilder();

            //  Is the property marked with [InjectProperty], and is it null?
            IsPropertyMarked = new DelegatePropertySelector((pi, obj) => {
                var shouldInject = pi.CustomAttributes.Any(
                    cad => cad.AttributeType.Name == nameof(InjectPropertyAttribute)
                    ) && pi.GetValue(obj) == null;
                return(shouldInject);
            });

            //  As each instance/type is activated, also inject marked properties.
            builder.RegisterCallback(crb => {
                crb.Registered += (_, regdArgs) => {
                    regdArgs.ComponentRegistration.Activated += (_, actdArgs) =>
                                                                actdArgs.Context.InjectProperties(actdArgs.Instance, IsPropertyMarked);
                };
            });

            return(builder);
        }