C#反思>;嵌套属性getValue>;对象与目标类型不匹配

C#反思>;嵌套属性getValue>;对象与目标类型不匹配,c#,reflection,attributes,nested,C#,Reflection,Attributes,Nested,我无法解决这个问题,所有的值都在对象的第一级得到很好的检索,但是嵌套的属性值会产生错误。我确信我需要调整getValue parameters对象,但我不确定如何调整。我试图将NestedPropertyInfo数组、propertyInfo或req作为参数传递,但仍然是相同的错误。有什么提示吗 UpdatePhoneReq req = new UpdatePhoneReq(); foreach (PropertyInfo propertyInfo in req.GetType().Get

我无法解决这个问题,所有的值都在对象的第一级得到很好的检索,但是嵌套的属性值会产生错误。我确信我需要调整getValue parameters对象,但我不确定如何调整。我试图将NestedPropertyInfo数组、propertyInfo或req作为参数传递,但仍然是相同的错误。有什么提示吗

UpdatePhoneReq req = new UpdatePhoneReq();   
foreach (PropertyInfo propertyInfo in req.GetType().GetProperties())
{
    if (propertyInfo.CanRead)
    {
        string attributValue = "";
        string attributName = propertyInfo.Name;
        Type attributType = propertyInfo.PropertyType;
        if (attributType == typeof(XFkType))
        {
            PropertyInfo[] nestedpropertyInfoArray = propertyInfo.PropertyType.GetProperties();
            attributValue += "{";
            foreach (PropertyInfo subProperty in nestedpropertyInfoArray)
            {
                // --- > the GetValue below generates the error
                System.Diagnostics.Debug.WriteLine(subProperty.Name + "=" + subProperty.GetValue(req, null).ToString());
                attributValue += subProperty.Name + "=" + ",";

            }
            attributValue = attributValue.Substring(0, attributValue.Length - 1) + "}";
        }
        else
            attributValue = propertyInfo.GetValue(req, null) == null ? "" : propertyInfo.GetValue(req, null).ToString();

            System.Diagnostics.Debug.WriteLine("[" + propertyInfo.PropertyType + "]" + attributName + "=" + attributValue);
    }
}

若要传递父对象“req”以获取子属性的值,则需要首先找到属性的值并传递它以获取子属性的值

var propertyInfoValue = propertyInfo.GetValue(req, null);
然后将此
属性infovalue
用于
子属性

subProperty.GetValue(propertyInfoValue , null)