0

I am trying to find out how to list the properties of a class that is in a session variable, and having no luck

i know that i can instance the class like so in a generic handler

IAdvanceUser user = context.Session["advUser"] as IAdvanceUser;

or like this in an aspx

IAdvanceUser user = new.SunGardHE.WebFramework.Security.AdvanceUser();

but i can seem to get reflection to work from this SO How to get the list of properties of a class?

IAdvanceUser user = new.SunGardHE.WebFramework.Security.AdvanceUser();
foreach(var prop in user.GetType().GetProperties()) {
    Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(user, null));
}

it doesnt error, but doesnt show any output either

Community
  • 1
  • 1
Jay Rizzi
  • 4,196
  • 5
  • 42
  • 71
  • Your object `user` is of type `IAdvanceUser`, Your code would only show you the properties exist in the interface, and I think you don't have any in your interface. If you need class properties then store/cast the object to your class type and then use reflection. – Habib Oct 07 '14 at 15:08
  • Try `foreach(var prop in new.SunGardHE.WebFramework.Security.AdvanceUser().GetType().GetProperties())` – Habib Oct 07 '14 at 15:24
  • gives the error CS1031: Type expected – Jay Rizzi Oct 07 '14 at 15:32
  • @JayRizzi there is nothing wrong with the code you posted as long as there are public properties. Create a small test application with a dummy POCO or try different types. @Habib, `user` is still an instance of `AdvanceUser`. Calling `user.GetType()` will return `AdvanceUser`, not `IAdvanceUser`. – TyCobb Oct 07 '14 at 15:35
  • there are public as well as private properties, oh well, whatya gonna do – Jay Rizzi Oct 07 '14 at 17:03

0 Answers0