C# 通过反射调试可视化工具:使用反射获取复杂对象中包含的属性值

发布于 2024-10-16 17:46:05 字数 650 浏览 1 评论 0原文

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

IE:

lista_tipiclassifornitura[i].OpzioneTariffaria.Codice

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

string Spool(object c, string propertyPath)

我将按如下方式调用此方法:

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

该过程将 # 替换为 0, 1, 2, ecc 并且应该访问财产“OpzioneTariffaria”和本“Codice”的财产通过MemberInfo 获得。

这个例子可以帮助我吗? 使用反射获取嵌套对象属性值

有什么建议吗?

Suppose you have a list of 100 complex nested object, and you want to spool all the values of a propery (identified by a pattern) of some subobject.

IE:

lista_tipiclassifornitura[i].OpzioneTariffaria.Codice

The immediate windows doesn't support loop, so I want to create a public static method like this:

string Spool(object c, string propertyPath)

I'll call this method as follow:

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

The procedure replace the # with a 0, 1, 2, ecc and should access the Property "OpzioneTariffaria" and the property of this "Codice" through MemberInfo.

This example could help me?
Getting Nested Object Property Value Using Reflection

Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2024-10-23 17:46:05

是的!这是可以做到的。

方法如下:

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

Yes! It can be done.

This is how:

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文