在嵌套属性中使用 GetValue() 在反射中抛出 TargetException [英] TargetException thrown in Reflection using GetValue() in nested properties
问题描述
我需要获取每个对象的所有属性的 Name 和 Value.其中一些是引用类型,所以如果我得到以下对象:
I need to get the Name and Value of all properties of each object. Some of them are reference type so if I get the following objects:
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; }
}
当从 Album
对象获取属性时,我还需要获取属性 AlbumArtist.Id
和 AlbumArtist.Name
的值嵌套.
When getting the properties from the Album
object I would need also to get the values of properties AlbumArtist.Id
and AlbumArtist.Name
which are nested.
到目前为止,我有以下代码,但它在尝试获取嵌套代码的值时触发了 System.Reflection.TargetException.
I have the following code so far, but it triggers the System.Reflection.TargetException when trying to get the value of the nested ones.
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());
}
}
所以在 If
语句中,我只检查属性是否在我的引用类型的命名空间下,如果是,我应该获取所有嵌套的属性值,但这就是引发异常的地方.
So in the If
statement I just check if the property is under the namespace of my reference types, if it is I should get all the nested properties values but that's where the exception is raised.
推荐答案
这会失败,因为您试图在 PropertyInfo
实例上获取 Artist
属性:
This fails because you are trying to get an Artist
property on a PropertyInfo
instance:
if(subProperty.GetValue(property, null) != null)
valueNames.Add(subProperty.Name, subProperty.GetValue(property, null).ToString());
据我所知,您需要 Artist
实例中的值,该实例嵌套在 row
对象(它是一个 Album
实例)中.
As I understand you need the values from the Artist
instance which is nested inside the row
object (which is an Album
instance).
所以你应该改变这个:
if(subProperty.GetValue(property, null) != null)
valueNames.Add(subProperty.Name, subProperty.GetValue(property, null).ToString());
为此:
var propValue = property.GetValue(row, null);
if(subProperty.GetValue(propValue, null) != null)
valueNames.Add(subProperty.Name, subProperty.GetValue(propValue, null).ToString());
Full(稍作改动以避免在我们不需要时调用 GetValue)
Full (with a little change to avoid calling GetValue when we don't need)
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());
}
}
此外,您可能会遇到属性名称重复的情况,因此您的 IDictionary<,>.Add
将失败.我建议在这里使用更可靠的命名.
Also, you may run into situations when the property names are duplicated, so your IDictionary<,>.Add
will fail. I suggest to use something more reliable naming here.
例如:property.Name + "."+ subProperty.Name
这篇关于在嵌套属性中使用 GetValue() 在反射中抛出 TargetException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!