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

C# 使用字符串名称访问/更改变量,c#,reflection,C#,Reflection,我试图更改一个变量值,但我只有一个字符串形式的变量名。我知道这通常并不理想,但请假设这是我的情况所必需的。我要更改的变量类型为Element,我创建的一个类如下所示: public class Element { public string TopBoundReplace; public string RightBoundReplace; public string BottomBoundReplace; public Li

我试图更改一个变量值,但我只有一个字符串形式的变量名。我知道这通常并不理想,但请假设这是我的情况所必需的。我要更改的变量类型为
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;
        }
    }
因此,通常,我会通过如下操作编辑有问题的值:

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.DueDate.Value[0]=“2012年10月25日”

如何使用字符串值作为变量名?我试过一些反思的东西:

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

myType最终得到了正确的类型(
Element
),但在运行上述代码时myPropInfo仍然为空,因为“DueDate”不是
元素
类的属性,而是
ocrVar
类的属性。我想像上面的代码一样获取DueDate
元素中的值,这样我就可以处理这些值,然后将它们放回ocrVar.DueDate中。

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

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

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

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

嗯。。我认为守则应该是:

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

嗯。。我认为守则应该是:

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

您需要使用
FieldInfo
GetField
而不是
PropertyInfo
GetProperty
,因为您的
ocrVar.DueDate
是一个字段,而不是属性

此代码将
ocrVar.DueDate
字段获取到
元素
变量中。这就是你想做的吗

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

您需要使用
FieldInfo
GetField
而不是
PropertyInfo
GetProperty
,因为您的
ocrVar.DueDate
是一个字段,而不是属性

此代码将
ocrVar.DueDate
字段获取到
元素
变量中。这就是你想做的吗

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


你想要BillDate还是DueDate?您正在尝试获取名为DueDate的元素的属性,该属性不存在。我将使用BillDate或DueDate。最后,我会将它们都视为字符串,并希望访问它们中的每一个。我对我的问题进行了一些编辑,以表明我理解我的代码试图从PropertyInfo行的错误类中获取属性。请看这个答案-您想要的是BillDate还是DueDate可能是重复的?您正在尝试获取名为DueDate的元素的属性,该属性不存在。我将使用BillDate或DueDate。最后,我会将它们都视为字符串,并希望访问它们中的每一个。我对我的问题进行了一些编辑,以表明我理解我的代码试图从PropertyInfo行的错误类中获取属性;I get“非静态字段、方法或属性'object.GetType()'需要对象引用”我还尝试了类型myType=typeof(ocrVar),它在那一行工作,但我的propInfo仍然为空。OP的
DueDate
是一个字段,而不是一个属性。是的,我明白了。他想访问属性或字段。你看他的问题不清楚!当我尝试使用类型myType=ocrVar.GetType()时,我得到“非静态字段、方法需要对象引用,或者属性'object.GetType()'我也尝试了类型myType=typeof(ocrVar),它在那一行上工作,但是我的propInfo仍然为空。OP的
DueDate
是一个字段,不是属性。是的,我知道了。他希望访问属性或字段。你看不清楚他的问题!我用我的原始类型myType=ocrVar.BillDate.GetType()尝试了这个方法;并具有更新的类型myType=typeof(ocrVar);在这两种情况下,propInfo仍然为null;并具有更新的类型myType=typeof(ocrVar);在这两种情况下,propInfo仍然为空。是的,这就是我想要的。现在,一旦我操作了元素值中的值,我如何用元素值中的新值替换ocrVar.DueDate?您得到的
元素是ocrVar.DueDate-它是对同一对象的引用,而不是副本。所以你不需要更换它!是的,这就是我想要的。现在,一旦我操作了元素值中的值,我如何用元素值中的新值替换ocrVar.DueDate?您得到的
元素是ocrVar.DueDate-它是对同一对象的引用,而不是副本。所以你不需要更换它!