0

< I edit my question to be clear for everyone >

I have this Model/Class

public class Address
{
    public string address{ get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string code { get; set; }
}

I needing a List< String > that contains every property of 'Address', for example:

"address", "city", "state", "code"...

I already tried with Reflection but I've failed, it is returning properties of "LIST" not from my Model/Class

PropertyInfo[] propList = Address.GetType().GetProperties();
                    foreach (PropertyInfo prop in propList)
                    {
                        object propValue = prop.GetValue(propertyValue, null);
....

it is returning the following properties:

"Capacity", "Count", "Item[Int32]"
gui.ess
  • 9
  • 3
  • Retrieve them from where? Where does reflection come into it? – Sayse Mar 22 '16 at 15:26
  • 3
    It looks like you are trying to retrieve the properties **of a list**, instead of getting a list of properties of your `Address` class. Can you show how you extract the properties? – Lars Kristensen Mar 22 '16 at 15:30
  • Can you please post what you said you have tried? For future reference, post everything that encompasses your issue so that it can give all of us that are trying to help you the best understanding, which in return will give you the best result – Grizzly Mar 22 '16 at 15:30
  • Possible duplicate of [How to get the list of properties of a class?](http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class) – Win Mar 22 '16 at 15:32
  • I've already tried but it didnt work @win – gui.ess Mar 22 '16 at 15:42
  • @gui.ess Please create a console application, and test it yourself. – Win Mar 22 '16 at 16:00

2 Answers2

2

It is not clear what you want, but if what you want is a List<string> with the model properties:

var properties = typeof(Address).GetProperties().Select(p => p.Name).ToList();
Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
0

So you want a list of your properties? I currently don't understand exactly what kind of result you're expecting, usually using Reflection with the .GetProperties() method works like a charm. It returns a collection of PropertyInfo objects which contain all the information on the object - its name, type, even value if you have an object of your class instantiated. E.g.:

public class Program
{
    public static void Main()
    {
        var address = new Address();

        var type = address.GetType();

        var props = type.GetProperties();

        foreach(var property in props)
        {
            Console.WriteLine(property.Name);
        }
    }


}

public class Address
{
    public string address{ get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string code { get; set; }
}