   [.net] How to get all mambers from a class and print them on console..
  hey allIt seems my question is really low level qestion so i hope im in the right place and its not one of those stupid qustions.im learning c# so im new in this :-)my qestion isi have class that have some mambers in it (well da!!) enum carcompany    {   Mazda,   Honda,   Buick,   Audi    }    class Car{   private carcompany carco;   public carcompany Carco   {  get  { return carco;  }  set  { carco = value;  }   }    }now lets say i wish to print it so its not a big problemi do (lets say Car c1 = new Car();)Console.WriteLine(c1.Carco);my problem is lets say i will make a new mamber for the class and i will want to print it as well so i will need to update it manually (and i dont want that )i heard its called reflections how can i get all the class mambers from the class then print them on other methods in a way that if i will have a new mamber i will not have to update the print methodthanks all    
  You can use typeof() on a class to get the Type for that class. Once you have a Type, you can use GetFields() to get a collection of FieldInfos for that class. From there you can use the GetValue() method of FieldInfo to get the value for that field from a given instance.All that said, it's generally better to manually implement the print function rather than rely on reflection.   ;  (ooops it seems somebody already answered :). He is right - you should better not use reflection)There are two methods that come to mind. They both use reflection.First way: direct use of reflection. Second - reflection with helper classes.You do something like this:using System;using System.ComponentModel;using System.Reflection;namespace Stuff{    enum Brand    {   Bmw,   Renault    }    class Car    {   Brand brand;   public Brand Brand   {  get { return brand; }  set { brand = value; }   }    }    class Program    {   static void Main(string[] args)   {  Car car = new Car();  PrintToConsole(car);  Console.Write(new String('*', Console.WindowWidth));  PrintToConsoleSecondMethod(car);   }   static void PrintToConsole(object instance)   {  if (instance == null)  { throw new ArgumentNullException("instance");  }  Type type = instance.GetType();  PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);  foreach (PropertyInfo property in properties)  { Console.WriteLine("{0}.{1} = {2}",type.Name, property.Name, property.GetValue(instance, null));  }   }   static void PrintToConsoleSecondMethod(object instance)   {  if (instance == null)  { throw new ArgumentNullException("instance");  }  Type type = instance.GetType();  PropertyDescriptorCollection proerties = TypeDescriptor.GetProperties(instance);  foreach (PropertyDescriptor property in proerties)  { Console.WriteLine("{0}.{1} = {2}",type.Name, property.Name, property.GetValue(instance));  }   }    }}I prefer the second method, as it is easier to use. For such simple scenarios as yours, it doesn't matter.   ;Quote:Original post by SiCraneYou can use typeof() on a class to get the Type for that class. Once you have a Type, you can use GetFields() to get a collection of FieldInfos for that class. From there you can use the GetValue() method of FieldInfo to get the value for that field from a given instance.All that said, it's generally better to manually implement the print function rather than rely on reflection.can i get an example please?it seems so simple with that wayi mean the functionsGetValue() getfiels()(i just have no idea how to use them right now..)the other way of reflections is so hard for me to understand right now.. (probably to high lvl for starting .net programist :-) )and i wanted to mention that both the answers was a lot more then i hoped :-)   ;Quote:Original post by Skibulacan i get an example please?dilyan_rusev gave you one.   ;Quote:Original post by MachairaQuote:Original post by Skibulacan i get an example please?dilyan_rusev gave you one.yeah you rightat start it was really hard for me to understand what he talked about...but after a while and alot of testing i got it...thanks alot for every body for the help...i guess i will stay around this forum :-)