/// <summary> /// Converts a constant from one record data type to another record data type. /// </summary> /// <param name="originalValue">Original value to convert.</param> /// <param name="oldDataType">Old record data type.</param> /// <param name="newDataType">New record data type.</param> /// <returns>Converted constant.</returns> protected virtual object ConvertConstantStructuralRecord(object originalValue, StructuralDataType oldDataType, StructuralDataType newDataType) { if (oldDataType.Properties.Count != newDataType.Properties.Count) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The number of record entries on '{0}' and '{1}' does not match.", oldDataType, newDataType)); } var oldProperties = oldDataType.Properties; var newProperties = newDataType.Properties; var result = newDataType.CreateInstance(); ConstantsMap[originalValue] = result; // Allow cycles var n = newProperties.Count; for (var i = 0; i < n; i++) { var oldProperty = oldProperties[i]; var newProperty = newProperties[i]; var oldValue = oldProperty.GetValue(originalValue); var newValue = ConvertConstant(oldValue, oldProperty.Type, newProperty.Type); newProperty.SetValue(result, newValue); } return(result); }
protected override object ConvertConstantStructuralCore(object originalValue, StructuralDataType oldDataType, StructuralDataType newDataType) { var oldPropertyMap = oldDataType.Properties.ToDictionary(p => p.Name, p => p); var newProperties = newDataType.Properties; var count = newProperties.Count; var args = new object[count]; for (var i = 0; i < count; i++) { var newProperty = newProperties[i]; var oldProperty = oldPropertyMap[newProperty.Name]; var oldValue = oldProperty.GetValue(originalValue); var newValue = ConvertConstant(oldValue, oldProperty.Type, newProperty.Type); args[i] = newValue; } var res = newDataType.CreateInstance(args); return(res); }
protected override object ConvertConstantStructuralCore(object originalValue, StructuralDataType oldDataType, StructuralDataType newDataType) { var res = newDataType.CreateInstance(); ConstantsMap[originalValue] = res; // Allow cycles var oldPropertyMap = oldDataType.Properties.ToDictionary(p => p.Name, p => p); var newProperties = newDataType.Properties; for (int i = 0, n = newProperties.Count; i < n; i++) { var newProperty = newProperties[i]; var oldProperty = oldPropertyMap[newProperty.Name]; var oldValue = oldProperty.GetValue(originalValue); var newValue = ConvertConstant(oldValue, oldProperty.Type, newProperty.Type); newProperty.SetValue(res, newValue); } return(res); }
/// <summary> /// Converts a constant by instantiating the new data type based on the converted values of the original data type's properties, in declaration order. /// </summary> /// <param name="originalValue">Original value to convert.</param> /// <param name="oldDataType">Old data type.</param> /// <param name="newDataType">New data type.</param> /// <returns>Converted constant.</returns> /// <remarks>Anonymous types and tuple data types obey to the prerequisite of property ordering according to the constructor parameter list.</remarks> protected object ConvertConstantStructuralByPropertyOrderInstantiation(object originalValue, StructuralDataType oldDataType, StructuralDataType newDataType) { var oldProperties = oldDataType.Properties; var newProperties = newDataType.Properties; var n = newProperties.Count; var args = new object[n]; for (var i = 0; i < n; i++) { var oldProperty = oldProperties[i]; var newProperty = newProperties[i]; var oldValue = oldProperty.GetValue(originalValue); var newValue = ConvertConstant(oldValue, oldProperty.Type, newProperty.Type); args[i] = newValue; } var result = newDataType.CreateInstance(args); return(result); }