Reputation: 98
i made this recursive function, but after the "return" it jumps back to the line GetProp(classes, obj);
and so it returns twice and I get a wrong value.
Thats how I call the method
foreach (string fieldKey in fieldKeys)
{
Console.WriteLine(fieldKey);
var props = fieldKey.Split('.');
form.SetField(fieldKey, GetProp(props, inv));
}
Here is the method
public static string GetProp(string[] classes, object oldObj)
{
var obj = oldObj.GetType().GetProperty(classes[0]).GetValue(oldObj, null);
if(classes.Length>1)
{
classes = classes.Skip(1).ToArray();
GetProp(classes, obj);
}
return obj.ToString();
}
Upvotes: 1
Views: 133
Reputation: 481
You need the return at recursion i think.
public static string GetProp(string[] classes, object oldObj)
{
var obj = oldObj.GetType().GetProperty(classes[0]).GetValue(oldObj, null);
if(classes.Length>1)
{
classes = classes.Skip(1).ToArray();
return GetProp(classes, obj);
}
return obj.ToString();
}
Upvotes: 1
Reputation: 332
it doesn't jump this actually the most recent call returns to main call any way to fix this you need to return the most recent value just change GetProp(classes,object);
to return GetProp(classes,object);
Upvotes: 0
Reputation: 131
You need return the string when use recursive.
example:
public int recursive(int i){
if(i == 1){
i = i + 1;
return recursive(i);
}
return i;
}
try with
return GetProp(classes, obj);
Upvotes: 0