使用字符串获取对象属性

本文关键字:取对象 属性 获取 字符串 | 更新日期: 2023-09-27 18:36:25

ca我在这里看到了几个反思的例子,但似乎无法让它们为我的确切问题工作......

我看过的解决方案示例

我的3节课...

 public class Row
    {
        public string Cell1  = "cat";//{ get; set; }
        public string Cell2 = "fish"; //{ get; set; }
        public string Cell3 = "dog";  //{ get; set; }

    }
    public class Grid
    {
        public Row Row1 = new Row();
        public Row Row2 = new Row();
        public Row Row3 = new Row();
    }
    public class SetOfGrids
    {
        public Grid g1 = new Grid();
        public Grid g2 = new Grid();
    }

我如何获得价值...

SetOfGrids sog = new SetOfGrids();
 MessageBox.Show(sog.g1.Row1.Cell1);

我认为链接的 getproperty 函数可能有效...

public Object GetPropValue(String name, Object obj) {
    foreach (String part in name.Split('.')) {
        if (obj == null) { return null; }
        Type type = obj.GetType();
        PropertyInfo info = type.GetProperty(part);
        if (info == null) { return null; }
        obj = info.GetValue(obj, null);
    }
    return obj;
}

我想如何获得价值...(我收到空异常错误。

 string PathToProperty = "sog.g1.Row1.Cell1";
 string s = GetPropValue(PathToProperty, sog).ToString;
 MessageBox.Show(s);

结果应为"cat2"

更新将行代码更改为建议的以下内容。

public class Row
    {
        public string Cell1  { get; set; }
        public string Cell2 { get; set; }
        public string Cell3 { get; set; }
       public  Row()
        {
            this.Cell1 = "cat";
            this.Cell2 = "dog";
            this.Cell3 = "fish";
        }
    }

使用字符串获取对象属性

这个:

using System;
using System.Linq;
using System.Net;
using System.Reflection;
namespace Test
{
    public class Row
    {
        public string Cell1 { get; set; } 
        public string Cell2 { get; set; } 
        public string Cell3 { get; set; }  
    }
    public class Grid
    {
        public Row Row1 { get; set; } 
        public Row Row2 { get; set; } 
        public Row Row3 { get; set; } 
    }
    public class SetOfGrids
    {
        public Grid g1 { get; set; } 
        public Grid g2 { get; set; } 
    }
    class Program
    {
        public static object GetPropValue(string name, object obj)
        {
            foreach (string part in name.Split('.'))
            {
                if (obj == null) { return null; }
                var type = obj.GetType();
                var info = type.GetProperty(part);
                if (info == null) { return null; }
                obj = info.GetValue(obj, null);
            }
            return obj;
        }
        public static void Main()
        {
            SetOfGrids sog = new SetOfGrids
            {
                g1 = new Grid { Row1 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row2 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row3 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" } },
                g2 = new Grid { Row1 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row2 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" }, Row3 = new Row { Cell1 = "cat", Cell2 = "fish", Cell3 = "dog" } }
            };
            string PathToProperty = "g1.Row1.Cell1";
            object s = GetPropValue(PathToProperty, sog);
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}

工作正常。请注意,我将所有字段更改为实际属性并删除了字符串的第一部分,因为"sog."是变量名称,而不是属性。

问题是你要求方法GetProperty来获取字段,而不是属性。尝试将字段更改为属性或使用 GetField 方法。