public bool TryGetPropertyInitializer <TProperty>(string propertyName, out IPropertyInitializer <TMessage, TInput> initializer) { var inputPropertyInfo = typeof(TInput).GetProperty(propertyName); if (inputPropertyInfo != null) { var propertyType = typeof(TProperty); var inputPropertyType = inputPropertyInfo.PropertyType; // exactly the same type, we just copy it over unmodified if (inputPropertyType == propertyType) { initializer = new CopyPropertyInitializer <TMessage, TInput, TProperty>(propertyName); return(true); } // can only copy to object, no idea what the destination type would/could be if (propertyType == typeof(object)) { var type = typeof(CopyObjectPropertyInitializer <, ,>).MakeGenericType(typeof(TMessage), typeof(TInput), inputPropertyType); initializer = (IPropertyInitializer <TMessage, TInput>)Activator.CreateInstance(type, propertyName, propertyName); return(true); } if (PropertyInitializerCache.TryGetFactory <TProperty>(inputPropertyType, out var propertyConverter)) { initializer = propertyConverter.CreatePropertyInitializer <TMessage, TInput>(propertyName); return(true); } } initializer = null; return(false); }
public bool TryGetHeaderInitializer <TProperty>(string propertyName, out IHeaderInitializer <TMessage, TInput> initializer) { // headers use a double underscore prefix string inputPropertyName = new StringBuilder(propertyName.Length + 2).Append("__").Append(propertyName).ToString(); var inputPropertyInfo = typeof(TInput).GetProperty(inputPropertyName); if (inputPropertyInfo != null) { var propertyType = typeof(TProperty); var inputPropertyType = inputPropertyInfo.PropertyType; // exactly the same type, we just copy it over unmodified if (inputPropertyType == propertyType) { initializer = new CopyHeaderInitializer <TMessage, TInput, TProperty>(propertyName, inputPropertyName); return(true); } if (PropertyInitializerCache.TryGetFactory <TProperty>(inputPropertyType, out var propertyConverter)) { initializer = propertyConverter.CreateHeaderInitializer <TMessage, TInput>(propertyName, inputPropertyName); return(true); } } initializer = default; return(false); }
public void Nullable_to_non_nullable() { Assert.That(PropertyInitializerCache.TryGetFactory <int>(typeof(int?), out var converter), Is.True); Assert.That(converter.IsPropertyTypeConverter <int?>(out var typeConverter), Is.True); Assert.That(typeConverter, Is.TypeOf <FromNullableTypeConverter <int> >()); Assert.That(typeConverter.TryConvert(37, out int result), Is.True); Assert.That(result, Is.EqualTo(37)); }
public void Non_nullable_to_nullable_wider_type() { Assert.That(PropertyInitializerCache.TryGetFactory <long?>(typeof(int), out var converter), Is.True); Assert.That(converter.IsPropertyTypeConverter <int>(out var typeConverter), Is.True); Assert.That(typeConverter, Is.TypeOf <ToNullableTypeConverter <long, int> >()); Assert.That(typeConverter.TryConvert(37, out long?result), Is.True); Assert.That(result.Value, Is.EqualTo(37)); }
public async Task Task_nullable_to_non_nullable() { Assert.That(PropertyInitializerCache.TryGetFactory <int>(typeof(Task <int?>), out var converter), Is.True); Assert.That(converter.IsMessagePropertyConverter <Task <int?> >(out var typeConverter), Is.True); Assert.That(typeConverter, Is.TypeOf <AsyncConvertPropertyConverter <int, int?> >()); var context = MessageFactoryCache <SimpleRequest> .Factory.Create(new BaseInitializeContext(CancellationToken.None)); var result = await typeConverter.Convert(context, Task.FromResult((int?)37)); Assert.That(result, Is.EqualTo(37)); }
public void Non_nullable_to_wider_type() { Assert.That(PropertyInitializerCache.TryGetFactory <long>(typeof(int), out var converter), Is.True); Assert.That(converter.IsPropertyTypeConverter <int>(out var typeConverter), Is.True); Assert.That(typeConverter, Is.TypeOf <LongTypeConverter>()); }
public void No_converter_for_same_type() { Assert.That(PropertyInitializerCache.TryGetFactory <int>(typeof(int), out var converter), Is.False); }