示例#1
0
        static void Main(string[] args)
        {
            string tokenEnd = "%"; // must be on both sides of the item to detokenize
            string template = $"Firstname: {tokenEnd}FirstName{tokenEnd}. Age: {tokenEnd}Age{tokenEnd}. Created: {tokenEnd}Created{tokenEnd}";

            // using model
            Console.WriteLine("Using model");
            var model = new CustomModel
            {
                FirstName = "Anthony",
                Age       = 39,
                Created   = new DateTime(2019, 1, 1)
            };

            var result = Detokenizer.Detokenize(model, template, tokenEnd);

            Console.WriteLine(result);
            Console.WriteLine();

            // using dictionary
            Console.WriteLine("Using dictionary");
            var dictionary = new Dictionary <string, string>
            {
                { "FirstName", "Anthony" },
                { "Age", "39" },
                { "Created", new DateTime(2019, 1, 1).ToString() }
            };

            result = Detokenizer.Detokenize(dictionary, template, tokenEnd);
            Console.WriteLine(result);
            Console.WriteLine();



            // using dictionary with explicit token as part of dictionary items
            Console.WriteLine("Using dictionary with explicit token as part of dictionary items");
            dictionary = new Dictionary <string, string>
            {
                { $"{tokenEnd}FirstName{tokenEnd}", "Anthony" },
                { $"{tokenEnd}Age{tokenEnd}", "39" },
                { $"{tokenEnd}Created{tokenEnd}", new DateTime(2019, 1, 1).ToString() }
            };

            result = Detokenizer.Detokenize(dictionary, template);
            Console.WriteLine(result);


            Console.ReadLine();
        }
        public void Template_Is_Detokenized_With_Model()
        {
            string tokenEnd = "%%"; // must be on both sides of the item to detokenize
            string template = $"Firstname: {tokenEnd}FirstName{tokenEnd}. Age: {tokenEnd}Age{tokenEnd}. Created: {tokenEnd}Created{tokenEnd}";

            var model = new CustomModel
            {
                FirstName = "Anthony",
                Age       = 39,
                Created   = new DateTime(2019, 1, 1)
            };

            var result = Detokenizer.Detokenize(model, template, tokenEnd);

            var expected = $"Firstname: {model.FirstName}. Age: {model.Age}. Created: {model.Created}";

            Assert.Equal(expected, result);
        }
        public void Template_Is_Detokenized_With_Tokened_Dictionary()
        {
            string tokenEnd = "%%"; // must be on both sides of the item to detokenize
            string template = $"Firstname: {tokenEnd}FirstName{tokenEnd}. Age: {tokenEnd}Age{tokenEnd}. Created: {tokenEnd}Created{tokenEnd}";

            var dictionary = new Dictionary <string, string>
            {
                { $"{tokenEnd}FirstName{tokenEnd}", "Anthony" },
                { $"{tokenEnd}Age{tokenEnd}", "39" },
                { $"{tokenEnd}Created{tokenEnd}", new DateTime(2019, 1, 1).ToString() }
            };

            var result = Detokenizer.Detokenize(dictionary, template);

            var expected = $"Firstname: {dictionary[$"{tokenEnd}FirstName{tokenEnd}"]}. Age: {dictionary[$"{tokenEnd}Age{tokenEnd}"]}. Created: {dictionary[$"{tokenEnd}Created{tokenEnd}"]}";

            Assert.Equal(expected, result);
        }