Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用反射获取第二级属性值_C#_C# 4.0_Reflection_Extension Methods - Fatal编程技术网

C# 使用反射获取第二级属性值

C# 使用反射获取第二级属性值,c#,c#-4.0,reflection,extension-methods,C#,C# 4.0,Reflection,Extension Methods,我编写了一个扩展方法,用于获取对象的属性值。以下代码: public static string GetValueFromProperty(this object obj, string Name) { var prop = obj.GetType().GetProperty(Name); var propValue = prop != null ? (string)prop.GetValue(obj, null) : string.Empty; return propV

我编写了一个扩展方法,用于获取对象的属性值。以下代码:

public static string GetValueFromProperty(this object obj, string Name)
{
    var prop = obj.GetType().GetProperty(Name);
    var propValue = prop != null ? (string)prop.GetValue(obj, null) : string.Empty;
    return propValue;
}
它在一级属性上运行良好。现在我有一个问题。我想从下拉列表中选择文本,我称之为如下方法:

string s = drp.GetValueFromProperty("SelectedItem.Text");
但它不会返回任何东西

如何扩展从第二级属性(或通常以任何级别的形式)返回值的扩展方法

感谢快速代码(沿树向下遍历):

公共静态字符串GetValueFromProperty(此对象对象对象,字符串名称)
{
string[]Name=Name.Split('.');
对象currentObj=obj;
字符串值=null;
for(int i=0;i
快速编码(沿树向下遍历):

公共静态字符串GetValueFromProperty(此对象对象对象,字符串名称)
{
string[]Name=Name.Split('.');
对象currentObj=obj;
字符串值=null;
for(int i=0;i
您正在尝试查找名为
SelectedItem.Text
的属性,但该属性在给定对象上不存在(而且永远不会存在,
是一个保留字符,不能出现在属性名称中)

您可以解析输入,按
拆分每个方法,并将调用一个接一个地链接起来:

public static string GetValueFromProperty(this object obj, string Name)
{
  var methods = Name.Split('.');

  object current = obj;
  object result = null;
  foreach(var method in methods)
  {
    var prop = current.GetType().GetProperty(method);
    result = prop != null ? prop.GetValue(current, null) : null;
    current = result;
  }
  return result == null ? string.Empty : result.ToString();
}
活生生的例子

编辑:

交互setter方法看起来非常类似(我对要设置的属性类型进行了泛型):

public static void SetValueFromProperty(此对象对象对象,字符串名称,T值)
{
var methods=Name.Split('.');
目标电流=obj;
对象结果=空;
PropertyInfo prop=null;
对于(int i=0;i0)
prop=current.GetType().GetProperty(方法[methods.Length-1]);
if(null!=prop)
属性设置值(当前、值、空);
}

您正在尝试查找名为
SelectedItem.Text
的属性,但该属性在给定对象上不存在(而且永远不会存在,
是一个保留字符,不能出现在属性名称中)

您可以解析输入,按
拆分每个方法,并将调用一个接一个地链接起来:

public static string GetValueFromProperty(this object obj, string Name)
{
  var methods = Name.Split('.');

  object current = obj;
  object result = null;
  foreach(var method in methods)
  {
    var prop = current.GetType().GetProperty(method);
    result = prop != null ? prop.GetValue(current, null) : null;
    current = result;
  }
  return result == null ? string.Empty : result.ToString();
}
活生生的例子

编辑:

交互setter方法看起来非常类似(我对要设置的属性类型进行了泛型):

public static void SetValueFromProperty(此对象对象对象,字符串名称,T值)
{
var methods=Name.Split('.');
目标电流=obj;
对象结果=空;
PropertyInfo prop=null;
对于(int i=0;i0)
prop=current.GetType().GetProperty(方法[methods.Length-1]);
if(null!=prop)
属性设置值(当前、值、空);
}

谢谢,它工作正常。您能为这些属性的设置值添加代码吗?感谢塞特。请注意,我没有检查传递的字符串是否一致。谢谢,它工作正常。您可以为这些属性的设置值添加代码吗?感谢塞特。请注意,我没有检查传递的字符串是否一致。
public static void SetValueFromProperty<T>(this object obj, string Name, T value)
{
  var methods = Name.Split('.');

  object current = obj;
  object result = null;
  PropertyInfo prop = null;
  for(int i = 0 ; i < methods.Length - 1  ; ++i)
  {
    var method = methods[i];
    prop = current.GetType().GetProperty(method);
    result = prop != null ? prop.GetValue(current, null) : null;
    current = result;
  }

  if(methods.Length > 0)
    prop = current.GetType().GetProperty(methods[methods.Length - 1]);
  if(null != prop)
      prop.SetValue(current, value, null);
}