1

I have the following function:

public IEnumerable<string> PropertiesFromType<T>(IEnumerable<T> input)

From the type (T), I would like to get the names of the properties.

I have tried the following:

var properties = typeof(T).GetProperties();
//var properties = input.GetType().GetGenericArguments()[0].GetProperties(); // Doesn't work either
foreach (var item in properties)
{
    Console.WriteLine(item.Name);
}

// By input it does work
var inputProperties = input.First().GetType().GetProperties();
foreach (var item in inputProperties)
{
    Console.WriteLine(item.Name);
}

When sending an anonymous IEnumerable<object> into the function, it has no properties when retrieving the Type from T.

However, when using the Type of an item in the IEnumerable, it does have properties.

As suggested by: How to get the type of T from a member of a generic class or method? using the GetGenericArguments function neither returns properties.

Example: https://dotnetfiddle.net/Widget/uKzO6H

Edit: I guess what I was trying to say: Is it possible to get the Type of anonymous objects in an IEnumerable without having an instance of, by using the T.

I now realize that is impossible, as T will always be object for anonymous objects.

Leo
  • 19
  • 6
  • `foreach (string value in Enum.GetNames(typeof(T)))` – VDWWD Dec 31 '19 at 13:48
  • Does this answer your question? [When and where to use GetType() or typeof()?](https://stackoverflow.com/questions/11312111/when-and-where-to-use-gettype-or-typeof) – Pavel Anikhouski Dec 31 '19 at 15:45
  • @PavelAnikhouski Unfortunately not – Leo Dec 31 '19 at 16:36
  • @VDWWD See Pavel Anikhouski answer as to why that won't work – Leo Dec 31 '19 at 16:37
  • 1
    If you actually mean *anonymous types*, then no, they have proper properties as regular types does. In other words, if you feed it a collection of objects you constructed as `new { A = 42, B = "Meaning of life" }`, then you would find properties A and B on T. If you mean *dynamic* or just *`object`*, then you're right, you can't infer properties of each object from looking at the type of the collection, you need to query the type of each object. – Lasse V. Karlsen Jan 02 '20 at 14:55

1 Answers1

1

I'm not sure this is what you want, but the method below takes the first element of the list and returns it's properties.

public IEnumerable<string> PropertiesFromType<T>(IEnumerable<T> input)
{
    var item = input.First();
    var properties = new List<string>();

    foreach (PropertyInfo property in item.GetType().GetProperties())
    {
        properties.Add(property.Name);
    }

    return properties;
}

Usage example

public class Book
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime PublishDate { get; set; }
}

var PropertyList = PropertiesFromType<Book>(MyListOfBooks);
VDWWD
  • 35,079
  • 22
  • 62
  • 79
  • Thank you for your assistance, it is close to what I was trying to achieve, however that appears to be impossible (properties of anonymous objects, as they can be different with each element in an IEnumerable) – Leo Jan 02 '20 at 14:40