3

I am trying to create a generic method for my search, but I don't know how to return list of fields from my class.

Let's say I've got a class:

public class Table
    {
        [Key]
        public int ID { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }
    }

And now I want to return a list that would look like this:

"ID"
"Name"
"Address"

How do I do that?

tried something like this:

 FieldInfo[] fields = typeof(T).GetFields(
            BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            string[] names = Array.ConvertAll<FieldInfo, string>(fields,
                delegate(FieldInfo field) { return field.Name; });

But it has some unnecessary text after field names

EDIT

It's not duplicate because in my situation GetProperties().Select(f => f.Name) made a difference

Piotr P
  • 67
  • 1
  • 2
  • 11
  • 2
    The term you're looking for to aide in searching is "reflection." The easiest thing to experiment with is `typeof(Table).GetProperties()`, which returns a collection of `PropertyInfo` objects. – Cᴏʀʏ Aug 31 '16 at 16:57
  • And in particular, you're looking for the *properties* of the class. (The fields backing those properties will have compiler-generated names...) – Jon Skeet Aug 31 '16 at 16:59

3 Answers3

13

You can do this with reflection:

var listOfFieldNames = typeof(Table).GetProperties().Select(f => f.Name).ToList();

Note that you obviously want the properties, not the fields. The term "fields" refers to the private (instance) members. The public getters/setters are called properties.

Graham
  • 7,431
  • 18
  • 59
  • 84
René Vogt
  • 43,056
  • 14
  • 77
  • 99
2

You could write a utility function which gets names of properties in a given class:

static string[] GetPropertyNames<T>() =>
    typeof(T)
        .GetProperties()
        .Select(prop => prop.Name)
        .ToArray();

Alternatively, you can provide an extension method on the Type class and then equip the type itself with that feature:

static class TypeExtensions
{
    public static string[] GetPropertyNames(this Type type) =>
        type
            .GetProperties()
            .Select(prop => prop.Name)
            .ToArray();
}

...

foreach (string prop in typeof(Table).GetPropertyNames())
    Console.WriteLine(prop);

This code prints the three property names of the Table type:

ID
Name
Address
Zoran Horvat
  • 10,924
  • 3
  • 31
  • 43
1

You're looking to use what's known as reflection. You can get an array of PropertyInfo objects in the following way:

PropertyInfo[] properties = typeof(Table).GetType().GetProperties();

The PropertyInfo class contains information about each property in the class, including their names (which is what' you're interested in). There are many, many other things that you can do with reflection, but this is definitely one of the most common.

EDIT: Changed my answer to not require an instance of Table.

Eric Sondergard
  • 565
  • 5
  • 22
  • 1
    Here, each property (as in foreach(PropertyInfo propertyin properties)) will include things like name, type, getvalue() - all kinds of stuff you can use to make CSV, XML, DataTables, SQL INSERTS, cast classes - whatever. It's a treasure trove of information! – Shannon Holsinger Aug 31 '16 at 17:03
  • 2
    Note that `Table.GetType()` won't compile. `GetType()` is a _non-static_ instance method of an object. To get the `Type` instance for a type use `typeof()` – René Vogt Aug 31 '16 at 17:04
  • Yeah, missed that. It's typeof(Table) – Shannon Holsinger Aug 31 '16 at 17:04