LSL.YamlDotNet.VariableReplacement - Example

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using LSL.VariableReplacer;
using LSL.YamlDotNet.VariableReplacement;
using YamlDotNet.Core;
using YamlDotNet.Serialization;

public class Program
{
	public static void Main()
	{
		var replacer = new VariableReplacerFactory()
			.Build(c => c.AddVariables(new Dictionary<string, object>
			{
				["FirstName"] = "Al",
				["LastName"] = "Jones",
				["FieldName"] = "Name",
				["FullName"] = "$(FirstName) $(LastName)"
			}));

		var deserialiser = new DeserializerBuilder()
			.Build();

		var result = deserialiser.Deserialize<MyTest>(
			new VariableReplacerParser(replacer.ReplaceVariables, new Parser(new StringReader(
			// Note the use of variables in keys as well as values
			"""
			$(FieldName): $(FullName)
			Actual$(FieldName): $(FullName)
			Other: $(FullName)
			"""
			)))
		);
		
		result.Dump();
	}
	
    internal class MyTest
    {
        public string Name { get; set; }
        public string ActualName { get; set; }
        public string Other { get; set; }
    }	
}
;