public void Bad_DateTimeOffset_String_Throws() { // Arrange const string formattedString = "this_is_not_a_valid_dto"; // Act Action action = () => RuntimeTypeConverter.ConvertType(formattedString, typeof(DateTimeOffset)); // Assert Assert.Throws <FormatException>(action); }
private static object ConvertTextToTargetType(string text, Type targetType) { try { return(RuntimeTypeConverter.ConvertType(text, targetType)); } catch (FormatException exception) { throw new InvalidQueryException("Query creation failed due to incompatible types.", exception); } }
public void Can_Convert_Enums() { // Arrange const string formattedString = "1"; // Act object result = RuntimeTypeConverter.ConvertType(formattedString, typeof(TestEnum)); // Assert Assert.Equal(TestEnum.Test, result); }
public void Bad_TimeSpanString_Throws() { // Arrange const string formattedString = "this_is_not_a_valid_timespan"; // Act Action action = () => RuntimeTypeConverter.ConvertType(formattedString, typeof(TimeSpan)); // Assert Assert.Throws <FormatException>(action); }
public void ConvertType_Returns_Value_If_Type_Is_Same() { // Arrange var val = new ComplexType(); Type type = val.GetType(); // Act object result = RuntimeTypeConverter.ConvertType(val, type); // Assert Assert.Equal(val, result); }
public void Can_Convert_TimeSpans() { // Arrange TimeSpan timeSpan = TimeSpan.FromMinutes(45); string stringSpan = timeSpan.ToString(); // Act object result = RuntimeTypeConverter.ConvertType(stringSpan, typeof(TimeSpan)); // Assert Assert.Equal(timeSpan, result); }
public void Can_Convert_DateTimeOffsets() { // Arrange var dto = new DateTimeOffset(new DateTime(2002, 2, 2), TimeSpan.FromHours(4)); string formattedString = dto.ToString("O"); // Act object result = RuntimeTypeConverter.ConvertType(formattedString, typeof(DateTimeOffset)); // Assert Assert.Equal(dto, result); }
private object ConvertAttrValue(object newValue, Type targetType) { if (newValue is JContainer jObject) { // the attribute value is a complex type that needs additional deserialization return(DeserializeComplexType(jObject, targetType)); } // the attribute value is a native C# type. object convertedValue = RuntimeTypeConverter.ConvertType(newValue, targetType); return(convertedValue); }
private void AssertCompatibleId(ResourceIdentifierObject resourceIdentifierObject, Type idType) { if (resourceIdentifierObject.Id != null) { try { RuntimeTypeConverter.ConvertType(resourceIdentifierObject.Id, idType); } catch (FormatException exception) { throw new JsonApiSerializationException(null, exception.Message, null, AtomicOperationIndex); } } }
public void ConvertType_Returns_Value_If_Type_Is_Assignable() { // Arrange var val = new ComplexType(); Type baseType = typeof(BaseType); Type iType = typeof(IType); // Act object baseResult = RuntimeTypeConverter.ConvertType(val, baseType); object iResult = RuntimeTypeConverter.ConvertType(val, iType); // Assert Assert.Equal(val, baseResult); Assert.Equal(val, iResult); }
public void ConvertType_Returns_Default_Value_For_Empty_Strings() { // Arrange var data = new Dictionary <Type, object> { { typeof(int), 0 }, { typeof(short), (short)0 }, { typeof(long), (long)0 }, { typeof(string), "" }, { typeof(Guid), Guid.Empty } }; foreach (KeyValuePair <Type, object> pair in data) { // Act object result = RuntimeTypeConverter.ConvertType(string.Empty, pair.Key); // Assert Assert.Equal(pair.Value, result); } }
/// <summary> /// Puts the attributes of the resource into the resource object. /// </summary> private void ProcessAttributes(IIdentifiable resource, IEnumerable <AttrAttribute> attributes, ResourceObject ro) { ro.Attributes = new Dictionary <string, object>(); foreach (AttrAttribute attr in attributes) { object value = attr.GetValue(resource); if (_settings.SerializerNullValueHandling == NullValueHandling.Ignore && value == null) { return; } if (_settings.SerializerDefaultValueHandling == DefaultValueHandling.Ignore && Equals(value, RuntimeTypeConverter.GetDefaultValue(attr.Property.PropertyType))) { return; } ro.Attributes.Add(attr.PublicName, value); } }
private Type TryResolveCommonType(QueryExpression left, QueryExpression right) { Type leftType = ResolveFixedType(left); if (RuntimeTypeConverter.CanContainNull(leftType)) { return(leftType); } if (right is NullConstantExpression) { return(typeof(Nullable <>).MakeGenericType(leftType)); } Type rightType = TryResolveFixedType(right); if (rightType != null && RuntimeTypeConverter.CanContainNull(rightType)) { return(rightType); } return(leftType); }
public void ConvertToStringFromNonSupportedType() { RuntimeTypeConverter cnv = new RuntimeTypeConverter(); object foo = cnv.ConvertTo(typeof(string), GetType()); }
public void ConvertFromNonSupportedType() { RuntimeTypeConverter cnv = new RuntimeTypeConverter(); object foo = cnv.ConvertFrom(12); }