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_ignore_methods() { var implType = DynamicTypesHelper.ImplementType(typeof(IInterfaceWithMethod)); var instance = (IInterfaceWithMethod)Activator.CreateInstance(implType); ((Action)instance.DoSomething).Should().Throw <NotImplementedException>(); }
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_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))); }
static Type CreateType(Type baseType, Type stateType) { TypeBuilder typeBuilder = GetTypeBuilder(baseType); IResolver resolver = CreateResolver(stateType); BuildConstructors(typeBuilder, baseType, stateType, EnsureDispatcherField(typeBuilder, baseType, stateType), EnsureTriggerType(typeBuilder, baseType, resolver)); var type = typeBuilder.CreateType(); DynamicTypesHelper.Save(); return(type); }
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); }
private static object CreateHot(this IConfigurationProvider provider, Type type, IConfigurationSource source, out IDisposable subscription) { if (!type.IsInterface) { throw new ArgumentException($"Unsupported type '{type.FullName}'. Only interfaces are supported."); } var wrapperType = DynamicTypesHelper.ImplementWrapperType(type); var wrapper = Activator.CreateInstance(wrapperType); var initialConfig = GetInitialConfig(provider, type, source); var observable = (IObservable <object>)GetConfigObservable(provider, type, source); DynamicTypesHelper.SetCurrentInstance(wrapper, initialConfig); subscription = observable.Subscribe(nextInstance => DynamicTypesHelper.SetCurrentInstance(wrapper, nextInstance)); return(wrapper); }
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)); }
static TypeBuilder GetTypeBuilder(Type baseType) { var moduleBuilder = DynamicTypesHelper.GetModuleBuilder(baseType.Assembly); return(moduleBuilder.DefineType(DynamicTypesHelper.GetDynamicTypeName(baseType), TA.Public, baseType)); }