Inspirel banner

Programming Distributed Systems with YAMI4

3.8.4 .NET

The following example shows how to find whether some named entry exists in the parameters object, and at the same time obtain the ``proxy'' object referring to that entry:

Parameters.Entry e = params.Find("first_name");

If the params object is filled according to previous examples, then there is an entry named ``first_name'' and the Parameter.Entry proxy refers to it:

if (e != null)
{
    System.Console.WriteLine(e.GetString());
}

If the content is being read in a truly exploratory way and the code has to be resistant not only to missing entries, but also to entries that have unexpected type, more elaborate check can be performed instead:

if (e != null && e.Type == Parameters.EntryType.STRING)
{
    System.Console.WriteLine(e.GetString());
}

In the above example the Type property returns the type name of the given entry. All possible type names are defined as a nested enumeration in the Parameters class:

public sealed class Parameters
{
    public enum EntryType
    {
        BOOLEAN,
        INTEGER,
        LONG,
        DOUBLE,
        STRING,
        BINARY,
        BOOLEAN_ARRAY,
        INTEGER_ARRAY,
        LONG_ARRAY,
        DOUBLE_ARRAY,
        STRING_ARRAY,
        BINARY_ARRAY,
        NESTED_PARAMETERS,
        NESTED_PARAMETERS_ARRAY
    }

    // ...
}

Some applications might have a need for custom serialization or completely general data handling schemes - then a switch statement with separate branches for all possible type names is a natural approach. In fact, a conceptually similar code is part of the YAMI4 library itself.

The type of the entry can be explored directly as well, which might be handy if it is already known that such entry exists:

Parameters.EntryType ageType = params.GetType("age");

The Parameter.Entry type offers all reading operations and they are used in exactly the same way as the reading operations of the parameters object itself, with the exception that there is no need for the entry name parameter - the given entry is already being referred to, so there is no need to look for it by name.

The entry name can be also extracted from the Parameters.Entry object:

string name = e.Name;