Using the ArgumentReader

The ArgumentReader abstracts the argument, and makes it easier to retrieve information from it. The information can be retrieved and interpreted from within our method to create the actual Person instance we want to return. Because we know the constructor takes a string and an int, we can expect the provided argument to be constructed in a way that allows us to retrieve those values.

[Converter]
public static Person PersonTypeConverter(ArgumentReader reader)
{
    string name = reader.Read<string>();
    int age = reader.Read<int>();

    return new Person(name, age);
}

Each component of the argument is being read in order. This means that once a component has been read, it can not be read again. It simply functions as a queue. This makes it so you do not have to specify any indices, but it also means you have to store the values in order. With the simple method, We can now support parameters of type Person. The input string could look like this: hello (Bob, 52), which would result in a new instance of Person, with name set to Bob, and age set to 52.

Note

When utilizing the Read() method, the ArgumentReader will recursively attempt to convert the given argument into the specified type. If the type is primitive, it already knows how to convert it, as it is supported by default. If a custom type is specified however, it'll convert it using a custom converter method instead.

Last updated