G
Guest
I've almost completed building a Model-View-Controller but have run into a
snag. When an event is fired on a form control I want to automatically
updated the "connnected" property in the Model. This works fine if all of
the properties are at the top (root level) of the model but I'd like to keep
them in nested classes to organize them better.
So, for example, part of my data model looks like this (simplified) :
public class MainClass
public class NestedClass1
public bool Property1
public string Property2
public class NestedClass2
.... and so on.
Outside of the model I instantiate an object of MainClass and then inside
the contstructor of MainClass I instantiate an object of NestedClass. So,
for example, 'ObjectMain' would be an instance of MainClass and
'ObjectNested1' would be an instance of NestedClass1. That all seems to work
fine.
I'm now trying to build a method to retrieve the current value of a given
property. I pass to this method the instance of the model and the name of
the property. It first looks in the root of MainClass to see if the property
is there. If it isn't then it starts cycling through the various nested
classes to see if it's in one of them. Once it finds the correct nested
class then it retrieves the value of the property.
But here's the dilemma: I can explicitly specify the name of the nested
class to retrieve the property value, such as:
property.GetValue(ObjectMain.ObjectNested1, null)
That works great! But I'd much prefer to keep things more generic so that
this method would be completely resuable in the future. Yet I cannot figure
out how to use Reflection (or something else) to get the equivalent of
'ObjectMain.ObjectNested1'
I realize this is a complicated issue but I hope I've explained it
succinctly and hope even more that someone has a solution!
Robert Werner
Vancouver, BC
snag. When an event is fired on a form control I want to automatically
updated the "connnected" property in the Model. This works fine if all of
the properties are at the top (root level) of the model but I'd like to keep
them in nested classes to organize them better.
So, for example, part of my data model looks like this (simplified) :
public class MainClass
public class NestedClass1
public bool Property1
public string Property2
public class NestedClass2
.... and so on.
Outside of the model I instantiate an object of MainClass and then inside
the contstructor of MainClass I instantiate an object of NestedClass. So,
for example, 'ObjectMain' would be an instance of MainClass and
'ObjectNested1' would be an instance of NestedClass1. That all seems to work
fine.
I'm now trying to build a method to retrieve the current value of a given
property. I pass to this method the instance of the model and the name of
the property. It first looks in the root of MainClass to see if the property
is there. If it isn't then it starts cycling through the various nested
classes to see if it's in one of them. Once it finds the correct nested
class then it retrieves the value of the property.
But here's the dilemma: I can explicitly specify the name of the nested
class to retrieve the property value, such as:
property.GetValue(ObjectMain.ObjectNested1, null)
That works great! But I'd much prefer to keep things more generic so that
this method would be completely resuable in the future. Yet I cannot figure
out how to use Reflection (or something else) to get the equivalent of
'ObjectMain.ObjectNested1'
I realize this is a complicated issue but I hope I've explained it
succinctly and hope even more that someone has a solution!
Robert Werner
Vancouver, BC