I am trying since a week to understand which design I could use to solve my problem. Couldnt get far and so I thought id ask here.
Scenario: I am trying to build a class which can have N properties of type:
- Example
- List [Example]
- string
- double.
These properties can be set by accessing a setter delegate stored inside a Dictionary. Since I need to access these by string I thought it would be ideal as I want to avoid reflection. The Example class is used to build a tree and my algorithm knows where to append the child by using string values.
Now I feel my way of doing things is not so elegant, here the code:
internal class Example {
internal string A;
internal List<Example> B = new List<Example>();
internal List<Example> C = new List<Example>();
internal double D;
internal Example E;
internal Dictionary<string, Action<object>> Map = new Dictionary<string, Action<object>>();
internal Example() {
Map.Add("A", SetA);
Map.Add("B", SetB);
Map.Add("C", SetC);
Map.Add("C", SetD);
Map.Add("E", SetE);
}
internal void SetA(object a) {
A = (string) a;
}
internal void SetB(object b) {
B.Add((Example) b);
}
internal void SetC(object c) {
C.Add((Example) c);
}
internal void SetD(object d) {
D = (double) d;
}
internal void SetE(object e) {
E = (double)e;
}
}
Then I can simply do:
myExampleInstance.Map["E"] (new Example())
How can I restructure my code so its maintainable and elegant?
Aucun commentaire:
Enregistrer un commentaire