C# 如何使用字符串属性路径获取/设置属性值?
假设我们有一个具有BasicDetails类属性的类根,然后BasicDetails有两个属性。字符串“Name”和动态对象“CustomAttributes” 我希望通过以下方式获取值:C# 如何使用字符串属性路径获取/设置属性值?,c#,.net,asp.net-core,reflection,C#,.net,Asp.net Core,Reflection,假设我们有一个具有BasicDetails类属性的类根,然后BasicDetails有两个属性。字符串“Name”和动态对象“CustomAttributes” 我希望通过以下方式获取值: var root = new Root(); root.BasicDetails.Name = "Samurai"; root.BasicDetails.CustomAttributes.phone = "12345"; string res1 = Ge
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();
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; }
}
以下是我尝试过的代码(基于SO的另一个答案):
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();
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; }
}
以下是课程:
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();
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; }
}
我尝试过的函数抛出空引用错误。不幸的是,我不太理解反射,所以目前我正在修补。如果有人能解释一下如何做到这一点,那就太好了。提前感谢。以下方法从嵌套属性获取值:
public static object GetPropertyValue(object src, string propName)
{
if (src == null) throw new ArgumentException("Value cannot be null.", "src");
if (propName == null) throw new ArgumentException("Value cannot be null.", "propName");
if (propName.Contains("."))//complex type nested
{
var temp = propName.Split(new char[] { '.' }, 2);
return GetPropertyValue(GetPropertyValue(src, temp[0]), temp[1]);
}
else
{
if (src is ExpandoObject)
{
var expando = src as IDictionary<string, object>;
if (expando != null)
{
object obj;
expando.TryGetValue(propName, out obj);
return obj;
}
return null;
}
else
{
var prop = src.GetType().GetProperty(propName);
return prop != null ? prop.GetValue(src, null) : null;
}
}
}
大家好,
ExpandoObject
来自哪里?似乎是missing@Scircia它是System.Dynamic
namespaceAh@PavelAnikhouski中的一个内置类。我的坏哈哈。@SamuraiJack,问题来自src.GetType().GetProperty(propName)。GetValue(src,null)
由于GetProperty
将在ExpandoObject
上返回null,这将导致NullReferenceException。德里克很好地解释了这一点。您需要更改GetDeepPropertyValue
的逻辑,并在调用GetValue
之前检查src.GetType().GetProperty(propName)
是否返回任何内容。如果它没有得到值,则需要创建一个检查来确定什么是src,并基于该值获取它的值value@SamuraiJack,我附上了一个工作代码示例。对于ExpandoObject,需要另一个解决方案。它可以完美地工作!我明白,对于expando对象,我需要以不同的方式处理它,但即使存在另一个类对象而不是ExpandoObject,解决方案也不起作用。我想知道为什么/你也有打字错误,属性“phone”是通过设置用小写字母写的,通过获取它是大写字母。