mardi 2 mars 2021

Replace template Placeholder with Object Properties with Reflection

In C#, I want to replace the string Placeholder with Object Properties using Reflection

        string formula = "{\"Name\": \"\", \"Email\": \"\" }";
        Student student = new Student();
        student.Name = "Parker";
        student.Email = "Parker@xyz.com";
        student.Address = "Mark Avenue";
        var result1 = GenerateJson(formula, student);
        //Output :  "{\"Name\": \"Parker\", \"Email\": \"Parker@xyz.com\" }"

        student.Name = "Royal";
        student.Email = "Royal@xyz.com";
        student.Address = "Cross Lane";
        var result2 = GenerateJson(formula, student);
        //Output :  "{\"Name\": \"Royal\", \"Email\": \"Royal@xyz.com\" }"





    public string GenerateJson(string formula, Student student)
    {
        string result = "";
        //logic for replacing the Placeholder woth object properties
        return result;
    }

    class Student
    {
        public string Name { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }

    }




Aucun commentaire:

Enregistrer un commentaire