How to set a property value by reflection in C# ?

There are times when you want to set the property of an object using reflection in C#. Below is a sample code snippet demonstrating how to do it taking the Employee class as an example and Name as the property.

How to set a property value by reflection in C# ?

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.Linq;

namespace ACCode
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee employee = new Employee();
            string name = "Test Name 1";
            // use reflection to set the value of the property name.
            PropertyInfo propertyInfo = employee.GetType().GetProperty("Name");
            propertyInfo.SetValue(employee, name);
            Console.WriteLine(employee.Name);
            Console.ReadLine();
        }
    }

    public class Employee
    {
        public string Name { get; set; }
    }
}