【问题标题】:access/change variable with string name使用字符串名称访问/更改变量
【发布时间】:2012-09-05 22:38:27
【问题描述】:

我正在尝试更改变量值,但我只有变量名作为字符串。我知道这通常并不理想,但请假设这对我的情况是必要的。我要更改的变量是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 仍然为空,因为“DueDate”不是 Element 类的属性,而是 ocrVar 类的属性。我想做类似上面的代码来获取 DueDate Element 中的值,这样我就可以操纵这些值,然后将它们放回 ocrVar.DueDate。

【问题讨论】:

  • 您想要 BillDate 还是 DueDate?您正在尝试获取 Element 的一个名为 DueDate 的属性,该属性不存在..
  • 我会选择 BillDate 或 DueDate。我最终会将它们都视为字符串,并希望访问它们中的每一个。我稍微编辑了我的问题,以表明我理解我的代码试图从 PropertyInfo 行上的错误类中获取属性。

标签: c# reflection


【解决方案1】:

您需要使用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

【讨论】:

  • 是的,这就是我想要的。现在,一旦我操纵了 Element 值中的值,如何将 ocrVar.DueDate 替换为 Element 值中的新值?
  • Element 你有 is ocrVar.DueDate - 它是对同一个对象的引用,而不是副本。所以你不需要更换它!
【解决方案2】:

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

    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();我得到“非静态字段、方法或属性‘object.GetType()’需要对象引用
  • 我也尝试过 Type myType = typeof(ocrVar),它适用于该行,但我的 propInfo 仍然为空。
  • OP 的 DueDate 是一个字段,而不是一个属性。
  • 是的,我明白了。他想要访问属性或字段。看他的问题就不清楚了!
【解决方案3】:

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

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

【讨论】:

  • 我用我原来的 Type myType = ocrVar.BillDate.GetType();并使用更新的 Type myType = typeof(ocrVar);在这两种情况下,propInfo 仍然为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多