Quantcast
Channel: Dynamics 365 Customer Engagement
Viewing all articles
Browse latest Browse all 286

Get the list of all properties of a class in C#

$
0
0
Hi,

Today we got a requirement to get the list of all the properties of a class including the values.

Here is a C# code for it.

classCustomer
{
     publicstring FirstName { get; set; }
     publicstring LastName { get; set; }
     publicstring MiddleName { get; set; }
}

classProgram
{
     staticvoid Main(string[] args)
     {
         // Creating an object for a class
         Customer objCustomer = newCustomer();
         objCustomer.FirstName = "John";
         objCustomer.LastName = "Hung";
         objCustomer.MiddleName = "Maro";

         // Reading the properties and displaying the Name and Value.
         foreach (var prop inobjCustomer.GetType().GetProperties())
         {
             Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(objCustomer, null));
         }
         Console.Read();
      }
}

Hope this helps.

--
Happy Coding

Gopinath

Viewing all articles
Browse latest Browse all 286

Trending Articles