C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 14 July 2016

How to Get Property Names of a class


Code
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 using System.Reflection;  
 namespace commongStringQuestion  
 {  
   class Program  
   {  
     public class A  
     {  
       public string Name { get; set; }  
       public string Id { get; set; }  
       private string x { get; set; }  
     }  
     static void Main(string[] args)  
     {  
       A obja = new A();  
       obja.Id = "1";  
       obja.Name = "Anurag";  
       string value = "changed value";  
       foreach (var prop in obja.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))  
       {  
         Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(obja, null));  
         prop.SetValue(obja, Convert.ChangeType(value, prop.PropertyType), null);  
       }  
       Console.WriteLine("-----------");  
       Console.WriteLine("Id : " + obja.Id);  
       Console.WriteLine("Name : " + obja.Name);  
       Console.ReadLine();  
     }  
   }  
 }  


No comments:

Post a Comment