Get value, type and names of properties and fields of a class

There are a ton of topics similar to this question, however, some of them are only for fields, the others are properties. I need a code snippet that retrieves values, types, and names of a class’s properties and fields. The following code works for only properties, not also for fields. I need both at once.

@Edit; If possible without loop, total number of properties and fields can be retrieved.

@Edit2; I think it can be done with .Count property.

What I’ve tried,

foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(item))
{
    string name = descriptor.Name;              // Name
    object value = descriptor.GetValue(item);   // Value
    var type = descriptor.PropertyType;         // Type
    Console.WriteLine($"{name}={value}={type}");
}

It outputs for the example class as,

humidity=abcd=System.String
temperature=123,12=System.Double
pressure=99=System.Int32

Example class,

class ExampClass
{
    public string testFieldJustField = "so";
    public string humidity { get; private set; }
    public double temperature { get; private set; }
    public int pressure { get; private set; }

    public ExampClass(string h, double t, int p)
    {
        humidity = h;
        temperature = t;
        pressure = p;
    }
}

Upvotes: 3

Views: 399

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

If you want to query without (explicit) loops, you can try Linq:

First we want all public instance properties, which can be read and are not indexers:

  using System.Linq;

  ...

  var item = new ExampClass("abcd", 123.12, 99);

  ...

  //TODO: Specify with a help of BindingFlags which properties do you want
  var props = item
    .GetType()
    .GetProperties(BindingFlags.Public | BindingFlags.Instance)
    .Where(pi => pi.CanRead)                      // can be read
    .Where(pi => !pi.GetIndexParameters().Any())  // not an indexer
    .Select(pi => new {
      name = pi.Name,
      value = pi.GetValue(pi.GetGetMethod().IsStatic ? null : item),
      type = pi.PropertyType,
      kind = "property",                          
    });

Second we want all public instance fields:

  //TODO: Specify with a help of BindingFlags which fields do you want
  var fields = item
    .GetType()
    .GetFields(BindingFlags.Public | BindingFlags.Instance)
    .Select(fi => new {
      name = fi.Name,
      value = fi.GetValue(fi.IsStatic ? null : item),
      type = fi.FieldType,
      kind = "field",
    });

Finally, we can combine both queries with a help of Concat:

  var result = props
    .Concat(fields)
    .OrderBy(record => record.name) // let's have results ordered
    .Select(record => $"{record.name}={record.value}={record.type}");

  // string.Join in order to avoid loops
  Console.WriteLine(string.Join(Environment.NewLine, result));

  // If you want total numbers put Count() 
  int total = props
    .Concat(fields)
    .Count();

  Console.WriteLine(total); 

Outcome:

  humidity=abcd=System.String
  pressure=99=System.Int32 
  temperature=123,12=System.Double
  testFieldJustField=so=System.String 
  4

Upvotes: 1

Related Questions