0

I've inherited from PictureBox and I have a lot of custom Properties set in place. One thing that I've grown intolerant of lately is the fact that I must always specify manually, each and every Property that I wish to output to the Console (to see its value, if any).

An example:

public class Picture : PictureBox
{
    public int Id { get; set; }

    public class PictureProperties
    {
        public string Name { get; set; }
        public string Extension { get; set; }
        public string Credits { get; set; }

        public Size DesignTimeSize { get; set; }
        public Point DesignTimePoint { get; set; }
    }

    public PictureProperties Properties { get; set; }
}

Usage:

public void test()
{
    SortedList<int, JTS.Picture> list = new SortedList<int, JTS.Picture>();

    for(int i = 0; i < 10; i++)
    {
        list.Add(1, new Picture()
            {
                Id = 1,
                Properties = new Picture.PictureProperties()
                {
                    Name = "Travis",
                    Credits = "people here"
                },

                Thumbnail = new Picture.PictureThumbnail()
                {
                    Size = new Size(250, 250)
                }
            });
    }
}

What I would like to do is, inside that foreach loop:

Console.WriteLine(Picture); and it will automagically output all of the properties contained within the Picture control. i.e. Name, Id, PictureProperties and its properties, Size, Location, absolutely everything. If you type, myNewPicture. intellisense will give you a list of all properties. That's what I want. All of that. In the console (or in a variable/string to output to file).

Is there a way to do this?

spike.y
  • 389
  • 5
  • 17
  • 1
    yes you can. Sorry i can't post an answer right now (site won't let anyone post answers atm), but you need to look at Reflection. Here's another question that is the same with an answer on it: http://stackoverflow.com/questions/6445045/c-sharp-getting-all-the-properties-of-an-object – MaxOvrdrv May 05 '14 at 17:14
  • @MaxOvrdrv perfect! Thank you very much Max. If you want to make an answer when the site will allow it I'll come back to accept. – spike.y May 05 '14 at 17:16
  • 1
    This is a duplicate question. Google "all properties of a class c#" and http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class is right at the top. – catfood May 05 '14 at 18:54

1 Answers1

1

yes you can. You have 2 options:

1: override your object's ToString() method to output the properties you want to show

2: Take a look at Reflection. Here's another question that is the same with an answer on it:
c# getting ALL the properties of an object

Community
  • 1
  • 1
MaxOvrdrv
  • 1,780
  • 17
  • 32