Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert any class to a keyvaluepair

I have a number (maybe a lot) of classes that are simple like so

 public class ResultA
    {
        public DateTime Date { get; set; }
        public int Year { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
    }

 public class ResultB
    {
        public string Number { get; set; }
        public int Count { get; set; }
        public string Update { get; set; }
        public int Jewels{ get; set; }
    }

There is no common interface, but they don't have methods simply properties.

I would like to be able to convert any type like this into a KeyValuePair<string,string> with the property name and the value if it is set.

Is there anyway of doing this horrible thing!?

like image 823
Loofer Avatar asked Jun 10 '15 11:06

Loofer


People also ask

What is the default value of KeyValuePair?

default equals to null. And default(KeyValuePair<T,U>) is an actual KeyValuePair that contains null, null .

How do you declare a key value pair?

To add key-value pair in C# Dictionary, firstly declare a Dictionary. IDictionary<int, string> d = new Dictionary<int, string>(); Now, add elements with KeyValuePair.


2 Answers

Peter beat me to it, but here is a quick linqpad:

void Main()
{
    GetValues(new ResultA
    {
        Date = DateTime.Now,
        Year = 2000
    }).Dump();
}

public IDictionary<string, string> GetValues(object obj) 
{
    return obj
            .GetType()
            .GetProperties()
            .ToDictionary(p=>p.Name, p=> p.GetValue(obj).ToString());
}

public class ResultA
{
    public DateTime Date { get; set; }
    public int Year { get; set; }
    public int Month { get; set; }
    public int Day { get; set; }
}

Output

Key   Value
Date  10-Jun-15 14:48:11 
Year  2000 
Month 0 
Day   0 
like image 74
Andrei Tătar Avatar answered Sep 30 '22 01:09

Andrei Tătar


Use reflection like this:

[Test]
public void DoStuff() {
  List<object> things = new List<object>() {
    new ResultA(){Date = DateTime.Now, Month = 34}, new ResultB(){Count = 1, Jewels = 4, Number = "2", Update = "0"}
  };

  foreach (var thing in things) {
    foreach (var property in thing.GetType().GetProperties()) {
      Trace.WriteLine(property.Name + " " + property.GetValue(thing));
    }
  }
}

Output:

Date 10.06.2015 13:46:41
Year 0
Month 34
Day 0
Number 2
Count 1
Update 0
Jewels 4

You can also use a extension method:

public static class ObjectExtensions {
  public static List<KeyValuePair<string, object>> GetProperties(this object me) {
    List<KeyValuePair<string, object>> result = new List<KeyValuePair<string, object>>();
    foreach (var property in me.GetType().GetProperties()) {
      result.Add(new KeyValuePair<string, object>(property.Name, property.GetValue(me)));
    }
    return result;
  }
}

Usage:

  [Test]
  public void DoItWithExtensionMethod() {
    List<object> things = new List<object>() {
    new ResultA(){Date = DateTime.Now, Month = 34}, new ResultB(){Count = 1, Jewels = 4, Number = "2", Update = "0"}
    };

    foreach (var thing in things) {
      var properties = thing.GetProperties();
      foreach (var property in properties) {
        Trace.WriteLine(property.Key + " " + property.Value);
      }
    }
  }
like image 33
Peter Punzenberger Avatar answered Sep 30 '22 01:09

Peter Punzenberger