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.
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
static void Main(string[] args)
{
// Creating an object for a class
Customer objCustomer = new Customer();
objCustomer.FirstName = "John";
objCustomer.LastName = "Hung";
objCustomer.MiddleName = "Maro";
{
Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(objCustomer, null));
}
Console.Read();
}
}
Hope this helps.
--
Happy Coding
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.
class Customer
{public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
}
class Program
{static void Main(string[] args)
{
// Creating an object for a class
Customer objCustomer = new Customer();
objCustomer.FirstName = "John";
objCustomer.LastName = "Hung";
objCustomer.MiddleName = "Maro";
// Reading the properties and displaying the Name and
Value.
foreach (var prop in
objCustomer.GetType().GetProperties()){
Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(objCustomer, null));
}
Console.Read();
}
}
Hope this helps.
--
Happy Coding
Gopinath