Skip to content

Variables Within Variables

Variables can reference other variables. This allows for composing of your data and preventing repeated code.

An example

The following code shows us setting up a FirstName variable and a LastName variable. We also setup a FullName variable that uses both FirstName and LastName to resolve its content:

var replacer = new VariableReplacerFactory()
    .Build(c => c.AddVariables(new Dictionary<string, object>
    {
        ["FirstName"] = "Al",
        ["LastName"] = "Jones",
        ["FullName"] = "$(FirstName) $(LastName)"
    }));

var result = replacer.ReplaceVariables(
    "Hello $(FullName). Can I call you $(FirstName)?"
);
// result will be "Hello Al Jones. Can I call you Al?"

Cyclic References

When resolving a variable, if a cyclic dependency is detected then a CyclicReferenceException is thrown that highlights the cyclic path.

Note

Versions 1.0.30 and earlier would throw an ArgumentException

This example shows this in effect:

var replacer = new VariableReplacerFactory()
    .Build(c => c.AddVariables(new Dictionary<string, object>
    {
        ["FirstName"] = "Al",
        ["LastName"] = "Jones",
        ["FullName"] = "$(FirstName) $(LastName) $(Other)",
        ["Other"] = "Stuff $(Another)",
        ["Another"] = "$(FullName)"
    }));

// This wil throw an exception
var result = replacer.ReplaceVariables(
    "Hello $(FullName). Can I call you $(FirstName)?"
);