0

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.

Ben Walker
  • 2,037
  • 5
  • 34
  • 56
  • 1
    Do you want BillDate or DueDate? You're trying to get a property of Element called DueDate, which doesn't exist.. – Blorgbeard Sep 12 '12 at 21:04
  • I'll take BillDate or DueDate. I'll end up seeing both of them as strings, and will want to access each of them. I have edited my question a little to show that I understand my code is trying to get a property from the wrong class on the PropertyInfo line. – Ben Walker Sep 12 '12 at 21:11
  • look this answer - http://stackoverflow.com/a/1487881/775779 – Dmitry Khryukin Sep 12 '12 at 21:17
  • possible duplicate of [C# get and set property by variable name](http://stackoverflow.com/questions/11824362/c-sharp-get-and-set-property-by-variable-name) – nawfal Apr 27 '13 at 18:31

3 Answers3

2

You need to use FieldInfo and GetField instead of PropertyInfo and GetProperty, because your ocrVar.DueDate is a field, not a property.

This code gets the ocrVar.DueDate field into an Element variable. Is that what you want to do?

Type myType = typeof(ocrVar);
FieldInfo myPropInfo = myType.GetField("DueDate");
Element value = (Element)myPropInfo.GetValue(null);  // null because it's static
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • 1
    Yes, that is what I want. Now, once I've manipulated the values in Element value, how can I replace ocrVar.DueDate with its new values which I have in Element value? – Ben Walker Sep 12 '12 at 21:29
  • 1
    The `Element` you've got **is** ocrVar.DueDate - it's a reference to the same object, not a copy. So you don't need to replace it! – Blorgbeard Sep 12 '12 at 21:30
1

Hmm.. I think the code should be:

    Type myType = ocrVar.GetType();      
    PropertyInfo myPropInfo = myType.GetProperty("DueDate", BindingFlags.Static | BindingFlags.Public);

Correction

Sorry I tested it. (By the way: on fields you use 'GetField' instead of 'GetProperty'). Here is the right code:

public class ocrVar
{
    static ocrVar()
    {
        DueDate = new Element();
        AmountDue =new Element();
        BillDate = new Element();
    }

    public static Element DueDate { get;private set; }
    public static Element AmountDue { get; private set; }
    public static Element BillDate { get; private set; }
}

How to use:

    private static void Test()
    {
        ocrVar ocrVar = new ocrVar();
        Type myType = ocrVar.GetType();
        PropertyInfo myPropInfo = myType.GetProperty("DueDate", BindingFlags.Static | BindingFlags.Public);
    }
CodeTherapist
  • 2,776
  • 14
  • 24
0

GetProperty returns null because the default binding flags when searching for properties are BindingFlags.Instance | BindingFlags.Public. Since your property is static, you need:

PropertyInfo myPropInfo = myType.GetProperty("DueDate", BindingFlags.Static | BindingFlags.Public);
driis
  • 161,458
  • 45
  • 265
  • 341
  • I tried this with my original Type myType = ocrVar.BillDate.GetType(); and with an updated Type myType = typeof(ocrVar); In both cases, propInfo is still null. – Ben Walker Sep 12 '12 at 21:17