0

Let's say I have a class called Student where it has properties of ID, FirstName, LastName. I have a dictionary which stores some information of a particular student.

I want to do something like

var student = new Student();
var studentInfo = new Dictionary<string, string>();
studentInfo["ID"] = "12345";
studentInfo["FirstName"] = "John";
studentInfo["LastName"] = "Doe";

Now I want to assign the key to the attribute of the student. Since the keys of the dictionary are the attribute of the student. I want to assign the Value of the dictionary to the student depending on the key. Here's my pseudo code.

foreach (var studentInfoItem in studentInfo)
{
    student.studentInfoItem.Key = studentInfoItem.Value;
}

Is there any way to do this properly?

superninja
  • 3,114
  • 7
  • 30
  • 63
  • 2
    It would really help if you told us what you will be using this for, [not just what you are doing](https://meta.stackexchange.com/a/66378/135230). – Dour High Arch Sep 14 '19 at 00:03
  • 2
    Hrm this seems a little X/Y ish, i am not fully satisfied you are using the dictionary in a way that makes sense to start with, i mean your dictionary keys are the Property names of the student (which seems a little weird), then you are trying to assign the value to the key, which doesn't make sense either. Can you tell us what problem you are trying to solve so we can rule out other solutions – TheGeneral Sep 14 '19 at 00:03
  • @DourHighArch so the reason why i do this is that the values of the dictionary are coming from a database call. I found a way to get all the values with the keys using only one DB call. But the hard part is how to assign those values back to the object using a dictionary.... Another solution is to get the value one by one by calling the database 3 times in this example. It would work but it would be super slow. – superninja Sep 14 '19 at 00:09
  • @TheGeneral I have added above. Please let me know if there's any alternatives without slowing down the performance. – superninja Sep 14 '19 at 00:09
  • 1
    Ahh i see, this makes more sense now. – TheGeneral Sep 14 '19 at 00:10
  • 1
    Honestly the "right" way to do this is to type it out for each item, e.g. `student.Name = studentInfo["Name"]`. That way it is type safe and your dependencies are explicit. Maybe using Reflection will save some typing up front, but so what? You only have to type it out once. – John Wu Sep 14 '19 at 00:24

1 Answers1

1

You could use reflection.

This is not a full (or the most elegant) solution, it's just to point you in the right direction.

public static void Assign<T>(T target,Dictionary<string,string> source)
{
   var properties = typeof(T).GetProperties();

   foreach (var prop in properties)
   {
      if(source.TryGetValue(prop.Name,out var value))
      {
         prop.SetValue(target, value);
      }
   }
}

private static void Main(string[] args)
{
   var student = new Student();
   var studentInfo = new Dictionary<string, string>();
   studentInfo["ID"] = "12345";
   studentInfo["FirstName"] = "John";
   studentInfo["LastName"] = "Doe";
   Assign(student, studentInfo);
}

Note : This lacks basic sanity checks and fault tolerance, and should only be used as an example.

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141