Properties Collection

Hi all,

I wonder if one of you kind people would be able to point me in the right direction. I have a class with quite a few properties and I thought it would be quite useful to be able to iterate through the classes collections with a foreach loop and return information about each property (such as name, type etc) and its value . However, this is where I come unstuck as I can't find an out-of -box way to do this.

I guess I need a collection of properties, but I'm not sure how best to implement
it - can anyone advise

TIA

Martin.


Answer this question

Properties Collection

  • EricPaul

    Here is a little example that prints out all the types and there properties in the current assembly:


    Assembly assembly = Assembly.GetExecutingAssembly();

    foreach( Type type in assembly.GetTypes() )
    {
    Console.WriteLine( "Type found: {0}", type.Name );

    foreach( PropertyInfo propertyInfo in type.GetProperties() )
    {
    Console.WriteLine( "Property: {0} ({1})", propertyInfo.Name, propertyInfo.PropertyType.Name );
    }

    Console.WriteLine( string.Empty );
    }




  • AllanP

    Well if you would like a list of properties of one class then you can use reflection to obtain the information about a class...
    There are many tutorials online, just google for them.


  • Properties Collection