【发布时间】:2021-07-02 05:10:36
【问题描述】:
假设我们有一个具有 BasicDetails 类属性的类 Root,然后 basicDetails 有两个属性。一个字符串“Name”和一个名为“CustomAttributes”的动态对象。
我想通过以下方式获取值:
var root = new Root();
root.BasicDetails.Name = "Samurai";
root.BasicDetails.CustomAttributes.phone = "12345";
string res1 = GetDeepPropertyValue(root, "BasicDetails.CustomAttributes.Phone").ToString();
string res2 = GetDeepPropertyValue(root, "BasicDetails.Name").ToString();
以下是我尝试过的代码(基于 SO 上的另一个答案):
public static object GetDeepPropertyValue(object src, string propName)
{
if (propName.Contains('.'))
{
string[] Split = propName.Split('.');
string RemainingProperty = propName.Substring(propName.IndexOf('.') + 1);
return GetDeepPropertyValue(src.GetType().GetProperty(Split[0]).GetValue(src, null), RemainingProperty);
}
else
return src.GetType().GetProperty(propName).GetValue(src, null);
}
以下是类:
public class Root
{
public Root()
{
BasicDetails = new BasicDetails();
}
public BasicDetails BasicDetails { get;set;}
}
public class BasicDetails
{
public BasicDetails()
{
CustomAttributes = new ExpandoObject();
}
public string Name { get; set; }
public dynamic CustomAttributes { get; set; }
}
我尝试过的函数抛出空引用错误。不幸的是,我不太了解反射,所以目前我正在修补猴子。如果有人可以请解释c#如何做到这一点,那就太好了。提前谢谢你。
【问题讨论】:
-
您好,
ExpandoObject来自哪里?好像不见了 -
@Scircia 这是一个来自
System.Dynamic命名空间的内置类 -
啊@PavelAnikhouski 我明白了。我的坏哈哈。
-
@SamuraiJack,问题来自
src.GetType().GetProperty(propName).GetValue(src, null),因为GetProperty将在ExpandoObject上返回null,这将导致NullReferenceException。 Drik 在his answer 中解释得很好。您需要更改GetDeepPropertyValue的逻辑并在调用GetValue之前检查src.GetType().GetProperty(propName)是否返回任何内容价值 -
@SamuraiJack,我附上了一个工作代码示例。对于 ExpandoObject,需要另一种解决方案。
标签: c# .net asp.net-core reflection