【问题标题】:C# Debug Visualizer throug reflection: get value of property contained in complex object using ReflectionC# Debug Visualizer 通过反射:使用反射获取复杂对象中包含的属性的值
【发布时间】:2011-02-23 18:12:47
【问题描述】:

假设您有一个包含 100 个复杂嵌套对象的列表,并且您想要假脱机某个子对象的属性(由模式标识)的所有值。

IE:

lista_tipiclassifornitura[i].OpzioneTariffaria.Codice

即时窗口不支持循环,所以我想创建一个这样的公共静态方法:

string Spool(object c, string propertyPath)

我将调用这个方法如下:

Spool(lista_tipiclassifornitura, "lista_tipiclassifornitura[#].OpzioneTariffaria.Codice")

该过程将 # 替换为 0、1、2、ecc,并应通过 MemberInfo 访问属性“OpzioneTariffaria”和此“Codice”的属性。

这个例子可以帮助我吗? Getting Nested Object Property Value Using Reflection

有什么建议吗?

【问题讨论】:

    标签: c# debugging list reflection spool


    【解决方案1】:

    是的!可以的。

    是这样的:

    public class Address
    {
        private string _addressLine1;
        public string AddressLine1
        {
            get { return _addressLine1; }
            set { _addressLine1 = value; }
        }
    
        private string _addressLine2;
        public string AddressLine2
        {
            get { return _addressLine2; }
            set { _addressLine2 = value; }
        }
    
        private string _city;
        public string City
        {
            get { return _city; }
            set { _city = value; }
        }
    
        private string _state;
        public string State
        {
            get { return _state; }
            set { _state = value; }
        }
    
        private string _zip;
        public string Zip
        {
            get { return _zip; }
            set { _zip = value; }
        }
    }
    
    public class Employee
    {
        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
    
        private string _middleName;
        public string MiddleName
        {
            get { return _middleName; }
            set { _middleName = value; }
        }
    
        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    
        private Address _employeeAddress;
        public Address EmployeeAddress
        {
            get { return _employeeAddress; }
            set { _employeeAddress = value; }
        }
    }
    
    class Program
    {
        #region Variabili Globali
    
        static private Contesto contesto;
    
        static private string file = ConfigurationManager.AppSettings["PathFileLog"];
    
        static EniLoggerManager log;
    
        static private ArrayList ListaMessaggi = null;
    
        #endregion
    
        public static object GetPropertyValueComplete(object obj, string pattern)
        {
            pattern = "x.Lista[#].Opzione.Codice";
            List<string> s = new List<string>(pattern.Split('.'));
    
    
            return GetPropertyValueComplete(obj, s);
            return "";
        }
    
        private static object GetPropertyValueComplete(object obj, List<string> s)
        {
            s.RemoveAt(0);
    
            if (s.Count == 1)
                return GetPropertyValue(obj, s[0]);
    
    
            foreach (string s1 in s)
            {
                if(s1.Contains("#"))
                {
                    object propertyValue = GetPropertyValue(obj, s1.Substring(0, s1.IndexOf('[')));
                    List<object> list = new List<object>();
                    for(int i = 0; i < 100; i++)
                    {
                        try
                        {
                            object value = GetPropertyValue(propertyValue, "Items", i);
    
                            if (value != null)
                                return GetPropertyValueComplete(value, (string) s);
                        }
                        catch (Exception)
                        {
                            break;
                        }
                    }
                }
                else return GetPropertyValueComplete(obj, (string) s);
            }
        }
    
        public static object GetPropertyValue(object obj, string propertyName)
        {
            return GetPropertyValue(obj, propertyName, null);
        }
    
        public static object GetPropertyValue(object obj, string propertyName, int? index)
        {
            Type objType = obj.GetType();
            PropertyInfo prop = objType.GetProperty(propertyName);
            FieldInfo prop1 = objType.GetField(propertyName);
    
            if (prop == null && prop1 == null)
                throw new Exception(string.Format("Proprietà {0} non trovata nel tipo {1}", propertyName, objType));
            else if (prop != null)
            {
                object propertyValue;
                if (index == null)
                    propertyValue = prop.GetValue(obj, null);
                else
                    propertyValue = prop.GetValue(obj, new Object[] {index});
                return propertyValue;
            }
            else if(prop1 != null)
                return prop1.GetValue(obj);
            return null;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-23
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      相关资源
      最近更新 更多