I am trying to change a variable value, but I only have the name of the variable as a string. I understand this is not generally ideal, but please assume it's necessary for my situation. The variable I want to change is of type Element
, a class I created that looks like this:
public class Element
{
public string TopBoundReplace;
public string RightBoundReplace;
public string BottomBoundReplace;
public List<string> ConfidenceString {get; set;}
public string LeftBoundReplace { get; set; }
public string Regex { get; set; }
public string ReplaceString { get; set; }
public List<int> LeftBound { get; set; }
public List<int> TopBound { get; set; }
public List<int> BottomBound { get; set; }
public List<int> RightBound { get; set; }
public string Whitelist { get; set; }
public List<System.Drawing.Rectangle> Rectangle { get; set; }
public System.Drawing.Rectangle SourceBox { get; set; }
public List<string> Value { get; set; }
public Element()
: this("", "", "", new List<int>(), new List<int>(), new List<int>(),new List<int>(), "", new List<Rectangle>(), new System.Drawing.Rectangle(), new List<string>(), new List<string>())
{
}
public Element(string leftBoundReplace, string regex, string replaceString, List<int> leftBound, List<int> topBound, List<int> bottomBound, List<int> rightBound, string whitelist, List<System.Drawing.Rectangle> rectangle, System.Drawing.Rectangle sourceBox, List<string> value, List<string> confidenceString)
{
Regex = regex;
ReplaceString = replaceString;
LeftBound = leftBound;
TopBound = topBound;
BottomBound = bottomBound;
RightBound = rightBound;
Whitelist = whitelist;
Rectangle = rectangle;
SourceBox = sourceBox;
Value = value;
LeftBoundReplace = leftBoundReplace;
ConfidenceString = confidenceString;
}
}
The Elements are initialized in another class, called ocrVar which looks like this (trimmed for the purposes of this question):
public class ocrVar
{
public static Element DueDate = new Element();
public static Element AmountDue = new Element();
public static Element BillDate = new Element();
}
So, normally, I would edit the value in question by doing something like this:
ocrVar.DueDate.Value[0] = "10/25/2012";
How can I do this with just the string value as the variable name? I have tried some Reflection stuff:
Type myType = ocrVar.BillDate.GetType();
PropertyInfo myPropInfo = myType.GetProperty("DueDate");
myType ends up getting the correct type (Element
), but myPropInfo remains null when running the above code because "DueDate" is not a property of the Element
class, it's a property of the ocrVar
class. I would like to do something like the above code to get the values in the DueDate Element
so I could manipulate the values, then put them back into ocrVar.DueDate.