예제 #1
0
        public void Helper_should_add_attributes_to_property()
        {
            var implType = DynamicTypesHelper.ImplementType(typeof(ICustomInterface));
            var info     = implType.GetProperty(nameof(ICustomInterface.Number));

            info.GetCustomAttribute <CustomAttribute>().Should().NotBeNull();
        }
예제 #2
0
        public void Helper_should_implement_generic_interface_with_different_types_multiple_times()
        {
            var implType1 = DynamicTypesHelper.ImplementType(typeof(IGenericInterface <bool, int>));
            var implType2 = DynamicTypesHelper.ImplementType(typeof(IGenericInterface <int, bool>));

            implType1.GetInterfaces().Should().Contain(typeof(IGenericInterface <bool, int>));
            implType2.GetInterfaces().Should().Contain(typeof(IGenericInterface <int, bool>));
        }
예제 #3
0
        public void Helper_should_ignore_methods()
        {
            var implType = DynamicTypesHelper.ImplementType(typeof(IInterfaceWithMethod));

            var instance = (IInterfaceWithMethod)Activator.CreateInstance(implType);

            ((Action)instance.DoSomething).Should().Throw <NotImplementedException>();
        }
예제 #4
0
        public void Helper_should_add_attributes_to_type()
        {
            var implType = DynamicTypesHelper.ImplementType(typeof(ICustomInterface));

            var attributes = implType.GetCustomAttributes <CustomAttribute>().ToArray();

            attributes.Should().NotBeEmpty();
            attributes[0].Should().BeEquivalentTo(new CustomAttribute(typeof(ICustomInterface))
            {
                Number = 42, Name = "test"
            });
            attributes[1].Should().BeEquivalentTo(new CustomAttribute(typeof(CustomAttribute)));
        }
예제 #5
0
        public void Helper_should_add_attributes_with_array_arguments()
        {
            var expectation = new CustomAttribute(new[] { typeof(bool), typeof(AttributeTargets) }, true, AttributeTargets.All)
            {
                Strings = new[] { "asd", "xyz" },
                Numbers = new[] { 1, 2, 3, 4, 5 }
            };

            var implType = DynamicTypesHelper.ImplementType(typeof(ITestArrayInAttribute));

            var attribute = implType.GetCustomAttribute <CustomAttribute>();

            attribute.Should().BeEquivalentTo(expectation);
        }
예제 #6
0
        public InterfaceBinder(ISettingsBinderProvider binderProvider)
        {
            var implType        = DynamicTypesHelper.ImplementType(typeof(TInterface));
            var classBinderType = typeof(ClassStructBinder <>).MakeGenericType(implType);

            classBinder = Activator.CreateInstance(classBinderType, binderProvider);

            var bindMethod = classBinderType.GetMethod(nameof(ISettingsBinder.Bind));

            if (bindMethod == null)
            {
                throw new NullReferenceException($"Can't find '{nameof(ISettingsBinder.Bind)}' method on '{classBinderType.FullName}' type.");
            }

            var bindingResultWrapperType = typeof(SettingsBindingResultWrapper <,>).MakeGenericType(typeof(TInterface), implType);

            callBindMethod = node =>
            {
                var bindingResult        = bindMethod.Invoke(classBinder, new object[] { node });
                var bindingResultWrapper = Activator.CreateInstance(bindingResultWrapperType, bindingResult);
                return((SettingsBindingResult <TInterface>)bindingResultWrapper);
            };
        }
예제 #7
0
        public void Helper_should_implement_internal_interface()
        {
            var implType = DynamicTypesHelper.ImplementType(typeof(IInternalInterface));

            implType.GetInterfaces().Should().Contain(typeof(IInternalInterface));
        }