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(); }
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>)); }
public void Helper_should_ignore_methods() { var implType = DynamicTypesHelper.ImplementType(typeof(IInterfaceWithMethod)); var instance = (IInterfaceWithMethod)Activator.CreateInstance(implType); ((Action)instance.DoSomething).Should().Throw <NotImplementedException>(); }
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))); }
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); }
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); }; }
public void Helper_should_implement_internal_interface() { var implType = DynamicTypesHelper.ImplementType(typeof(IInternalInterface)); implType.GetInterfaces().Should().Contain(typeof(IInternalInterface)); }