在嵌套属性中使用 GetValue() 时在反射中抛出 TargetException

spe*_*tyy 3 .net c# reflection

我需要获取每个对象的所有属性的名称值。其中一些是引用类型,因此如果我得到以下对象:

public class Artist {
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Album {
    public string AlbumId { get; set; }
    public string Name { get; set; }
    public Artist AlbumArtist { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

从对象获取属性时,Album我还需要获取属性的值AlbumArtist.Id以及AlbumArtist.Name嵌套的属性。

到目前为止,我有以下代码,但在尝试获取嵌套代码的值时,它会触发System.Reflection.TargetException 。

var valueNames = new Dictionary<string, string>();
foreach (var property in row.GetType().GetProperties())
{
    if (property.PropertyType.Namespace.Contains("ARS.Box"))
    {
        foreach (var subProperty in property.PropertyType.GetProperties())
        {
            if(subProperty.GetValue(property, null) != null)
                valueNames.Add(subProperty.Name, subProperty.GetValue(property, null).ToString());
        } 
    }
    else
    {
        var value = property.GetValue(row, null);
        valueNames.Add(property.Name, value == null ? "" : value.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,在If语句中,我只是检查该属性是否位于引用类型的命名空间下,如果是,我应该获取所有嵌套属性值,但这就是引发异常的地方。

Pet*_*rfy 5

这会失败,因为您正在尝试获取Artist实例的属性PropertyInfo

if(subProperty.GetValue(property, null) != null)
    valueNames.Add(subProperty.Name, subProperty.GetValue(property, null).ToString());
Run Code Online (Sandbox Code Playgroud)

Artist据我了解,您需要来自嵌套在对象row(这是一个实例)内的实例的值Album

所以你应该改变这个:

if(subProperty.GetValue(property, null) != null)
    valueNames.Add(subProperty.Name, subProperty.GetValue(property, null).ToString());
Run Code Online (Sandbox Code Playgroud)

对此:

var propValue = property.GetValue(row, null);
if(subProperty.GetValue(propValue, null) != null)
    valueNames.Add(subProperty.Name, subProperty.GetValue(propValue, null).ToString());
Run Code Online (Sandbox Code Playgroud)

完整(稍作更改以避免在不需要时调用 GetValue)

var valueNames = new Dictionary<string, string>();
foreach (var property in row.GetType().GetProperties())
{
    if (property.PropertyType.Namespace.Contains("ATG.Agilent.Entities"))
    {
        var propValue = property.GetValue(row, null);
        foreach (var subProperty in property.PropertyType.GetProperties())
        {
            if(subProperty.GetValue(propValue, null) != null)
                valueNames.Add(subProperty.Name, subProperty.GetValue(propValue, null).ToString());
        } 
    }
    else
    {
        var value = property.GetValue(row, null);
        valueNames.Add(property.Name, value == null ? "" : value.ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,您可能会遇到属性名称重复的情况,因此您IDictionary<,>.Add将失败。我建议在这里使用更可靠的命名。

例如:property.Name + "." + subProperty.Name