使用字符串名称访问/更改变量

时间:2012-09-12 21:00:48

标签: c# reflection

我正在尝试更改变量值,但我只将变量的名称作为字符串。我知道这通常不是理想的,但请假设我的情况是必要的。我想要更改的变量是Element类型,我创建的类看起来像这样:

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;
        }
    }

元素在另一个类中初始化,名为ocrVar,看起来像这样(为了这个问题的目的而修剪):

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

因此,通常情况下,我会通过执行以下操作来编辑有问题的值:

ocrVar.DueDate.Value[0] = "10/25/2012";

如何只使用字符串值作为变量名称来执行此操作?我尝试了一些反思的东西:

Type myType = ocrVar.BillDate.GetType();
PropertyInfo myPropInfo = myType.GetProperty("DueDate");

myType最终得到正确的类型(Element),但运行上面的代码时myPropInfo仍为null,因为“DueDate”不是Element类的属性,它是{的属性{1}}课程。我想做一些类似上面的代码来获取DueDate ocrVar中的值,这样我就可以操作这些值,然后将它们放回到ocrVar.DueDate中。

3 个答案:

答案 0 :(得分:2)

您需要使用FieldInfoGetField代替PropertyInfoGetProperty,因为您的ocrVar.DueDate是字段,而非属性。

此代码将ocrVar.DueDate字段转换为Element变量。那是你想要做的吗?

Type myType = typeof(ocrVar);
FieldInfo myPropInfo = myType.GetField("DueDate");
Element value = (Element)myPropInfo.GetValue(null);  // null because it's static

答案 1 :(得分:1)

嗯......我认为代码应该是:

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

校正

抱歉,我测试了它。 (顺便说一句:在你使用'GetField'而不是'GetProperty'的字段上)。 这是正确的代码:

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; }
}

使用方法:

    private static void Test()
    {
        ocrVar ocrVar = new ocrVar();
        Type myType = ocrVar.GetType();
        PropertyInfo myPropInfo = myType.GetProperty("DueDate", BindingFlags.Static | BindingFlags.Public);
    }

答案 2 :(得分:0)

GetProperty返回null,因为搜索属性时的默认绑定标志为BindingFlags.Instance | BindingFlags.Public。由于您的财产是静态的,您需要:

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