The requirement stated that all properties would be mapped to different database fields, a string value , "UserDetail.MyName", would map to "FullName" in the table.
After working on some ideas and searching for an example, finally did this:
1. Start with a property that I need from a object
1: private readonly string propertyToGet = "Details.MyName";
2. Create the "dummy" classes that I would use
1: public class User
2: {
3: public DateTime Birth { get; set; }
4:
5: public UserDetail Details { get; set; }
6:
7: public User()
8: {
9: Details = new UserDetail();
10: }
11: }
12:
13: public class UserDetail
14: {
15: public string MyName { get; set; }
16:
17: public UserDetail()
18: {
19: MyName = "Jacobus Meintjes";
20: }
21: }
3. Create a recursive function to run through all properties and get the correct property value.
1: private string GetPropertyValue(object obj, string property)
2: {
3: if(property.Contains("."))
4: {
5: string currentProperty = property.Substring(0, property.IndexOf("."));
6: string nextProperty = property.Substring(property.IndexOf(".") + 1);
7: object p = obj.GetType().GetProperty(currentProperty).GetValue(obj, null);
8:
9: return GetPropertyValue(p, nextProperty);
10: }
11:
12: //Cast it to the required type.
13: //Maybe return a object instead of string and use an extension method to
14: //return the required type.
15: return (string) obj.GetType().GetProperty(property).GetValue(obj, null);
16: }
4. Initialise the User class and get the required property's value
1: public string GetMyNameFromDetailsClass()
2: {
3: User u = new User();
4:
5: return GetPropertyValue(u, propertyToGet);
6: }
It finally went into production with a slight change, but that's because I used a different technique to get the property value to replace.
Here is the completed code
1: using System;
2: using System.Collections.Generic;
3: using System.Data;
4: using System.Linq;
5: using System.Reflection;
6: using System.Text;
7:
8:
9: namespace ReflectionApp
10: {
11: class Program
12: {
13: private readonly string propertyToGet = "Details.MyName";
14:
15: /// <summary>
16: /// Application Start
17: /// </summary>
18: /// <param name="args">No args required</param>
19: static void Main(string[] args)
20: {
21: Program program = new Program();
22: Console.WriteLine("My name is: " + program.GetMyNameFromDetailsClass());
23: Console.ReadKey();
24: }
25:
26: public string GetMyNameFromDetailsClass()
27: {
28: User u = new User();
29:
30: return GetPropertyValue(u, propertyToGet);
31: }
32:
33: private string GetPropertyValue(object obj, string property)
34: {
35: if(property.Contains("."))
36: {
37: string currentProperty = property.Substring(0, property.IndexOf("."));
38: string nextProperty = property.Substring(property.IndexOf(".") + 1);
39: object p = obj.GetType().GetProperty(currentProperty).GetValue(obj, null);
40:
41: return GetPropertyValue(p, nextProperty);
42: }
43:
44: //Cast it to the required type.
45: //Maybe return a object instead of string and use an extension method to
46: //return the required type.
47: return (string) obj.GetType().GetProperty(property).GetValue(obj, null);
48: }
49: }
50:
51: public class User
52: {
53: public DateTime Birth { get; set; }
54:
55: public UserDetail Details { get; set; }
56:
57: public User()
58: {
59: Details = new UserDetail();
60: }
61: }
62:
63: public class UserDetail
64: {
65: public string MyName { get; set; }
66:
67: public UserDetail()
68: {
69: MyName = "Jacobus Meintjes";
70: }
71: }
72: }
Output from this is:
My name is: Jacobus Meintjes