ChangeType method

Object? ChangeType(
  1. Object? value
)

Change value to a value of compatible type.

The type of a simple value should match exactly or be convertible to the appropriate type. An array value has to be a single dimension (rank), contain at least one value and contain elements that exactly match the expected type. (We could relax this last requirement so that, for example, you could pass an array of Int32 that could be converted to an array of Double but that seems like overkill).

@param value The value. @return New value. @throws Exception the exception

Implementation

Object? ChangeType(Object? value) {
  if (this.IsArray) {
    this.ValidateValueAsArray(value);
    return value;
  } else if (value.runtimeType == this.Type) {
    return value;
  } else {
    try {
      if (this.Type is int) {
        Object? o = null;
        o = int.parse(value.toString());
        return o;
      } else if (this.Type == DateTime) {
//                DateFormat df = new SimpleDateFormat(
//                "yyyy-MM-dd'T'HH:mm:ss'Z'");
//                return df.parse(value + "");
        return DateTime.parse(value.toString());
      } else if (this.Type == bool) {
        return value.toString().toLowerCase() == "true";
      } else if (this.Type == String) {
        return value.toString();
      }
      return null;
    } on Exception {
      throw ArgumentException(
          "The value '$value' of type ${value?.runtimeType} can't be converted to a value of type ${this.Type}.");
    }
  }
}