J
John Duval
Hi Jan,
I was able to get the string "Hello from there" using this code:
string inputString =
"Namespace.Subnamespace.Helper.User.Settings.MyValue";
int lastDot = inputString.LastIndexOf('.');
string typeName = inputString.Substring(0, lastDot);
string propertyName = inputString.Substring(lastDot + 1);
Namespace.User user = new Namespace.User();
Type userType = user.Settings.GetType();
PropertyInfo pi = userType.GetProperty(propertyName);
object output = pi.GetValue(user.Settings, null);
There isn't really a problem with the embedded namespace that I saw.
By the way, this code isn't very robust, since it assumes a lot about
the input string. I'm not sure where you get your input string from,
but it seems fragile to me to have to parse the string to separate the
type from the property name. Perhaps the calling code already knows
this information? If not, you'd probably want to add some error
handling in case the property does not exist, or that kind of thing.
Hope that helps,
John
I was able to get the string "Hello from there" using this code:
string inputString =
"Namespace.Subnamespace.Helper.User.Settings.MyValue";
int lastDot = inputString.LastIndexOf('.');
string typeName = inputString.Substring(0, lastDot);
string propertyName = inputString.Substring(lastDot + 1);
Namespace.User user = new Namespace.User();
Type userType = user.Settings.GetType();
PropertyInfo pi = userType.GetProperty(propertyName);
object output = pi.GetValue(user.Settings, null);
There isn't really a problem with the embedded namespace that I saw.
By the way, this code isn't very robust, since it assumes a lot about
the input string. I'm not sure where you get your input string from,
but it seems fragile to me to have to parse the string to separate the
type from the property name. Perhaps the calling code already knows
this information? If not, you'd probably want to add some error
handling in case the property does not exist, or that kind of thing.
Hope that helps,
John