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.

4
  • 1
    Do you want BillDate or DueDate? You're trying to get a property of Element called DueDate, which doesn't exist.. Commented Sep 12, 2012 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. Commented Sep 12, 2012 at 21:11
  • look this answer - stackoverflow.com/a/1487881/775779 Commented Sep 12, 2012 at 21:17
  • possible duplicate of C# get and set property by variable name Commented Apr 27, 2013 at 18:31

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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!
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);
    }

4 Comments

When I try to use Type myType = ocrVar.GetType(); I get "An object reference is required for the non-static field, method, or property 'object.GetType()'
I also tried Type myType = typeof(ocrVar), which works on that line, but my propInfo remains null.
OP's DueDate is a field, not a property.
Yes I see. He is want to access property or a field. It's not clear if you look at his question!
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);

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.