Sign in to follow this  
Skibula

[.net] How to get all mambers from a class and print them on console..

Recommended Posts

hey all It 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 is i 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 problem i 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 method thanks all

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
(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.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
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.




can i get an example please?
it seems so simple with that way
i mean the functions
GetValue()
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 :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by Machaira
Quote:
Original post by Skibula
can i get an example please?


dilyan_rusev gave you one.


yeah you right
at 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 :-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this