C# 如何获取未知属性的值(部分已通过反射解决)

C# 如何获取未知属性的值(部分已通过反射解决),c#,reflection,properties,C#,Reflection,Properties,我有一个现有的c#应用程序要修改,需要通过一个未知属性的对象进行循环,并且通过反射解决了一半的问题 我试图用属性名和属性值填充字典。下面是代码,我已经描述了在***和***之间我需要什么 这是一个MVC5项目 private Dictionary<string, string> StoreUserDetails () { var userDetails = new Dictionary<string, string>();

我有一个现有的c#应用程序要修改,需要通过一个未知属性的对象进行循环,并且通过反射解决了一半的问题

我试图用属性名和属性值填充字典。下面是代码,我已经描述了在***和***之间我需要什么

这是一个MVC5项目

    private Dictionary<string, string> StoreUserDetails ()
    {      
      var userDetails = new Dictionary<string, string>();

      foreach (var userItem in UserItems)
      {
        var theType = userItem.GetType();
        var theProperties = theType.GetProperties();

        foreach (var property in theProperties)
        {
          userDetails.Add(property.Name, ***value of userItem property with this property name***);
        }
      }      
      return userDetails;
    }
private Dictionary StoreUserDetails()
{      
var userDetails=newdictionary();
foreach(UserItems中的var userItem)
{
var theType=userItem.GetType();
var theProperties=theType.GetProperties();
foreach(属性中的var属性)
{
添加(property.Name,***此属性名为***的userItem属性的值);
}
}      
返回用户详细信息;
}

非常感谢您的帮助。

您需要的是
PropertyInfo.GetValue()
方法:

示例

property.GetValue(userItem, null);
语法

public virtual Object GetValue(
    Object obj,
    Object[] index
)
参数

obj

类型:
System.Object

将返回其属性值的对象

索引

类型:
System.Object[]

索引属性的可选索引值。索引属性的索引是从零开始的。对于非索引属性,此值应null

返回值

类型:
System.Object


指定对象的属性值。

您要查找的是
PropertyInfo.GetValue()
方法:

示例

property.GetValue(userItem, null);
语法

public virtual Object GetValue(
    Object obj,
    Object[] index
)
参数

obj

类型:
System.Object

将返回其属性值的对象

索引

类型:
System.Object[]

索引属性的可选索引值。索引属性的索引是从零开始的。对于非索引属性,此值应null

返回值

类型:
System.Object


指定对象的属性值。

这是如何执行的。(顺便说一句,您的代码可能会在“dictionary key not being unique”(字典键不唯一)上出错,因为第二个userItem将尝试向字典添加相同的属性名。您可能需要一个
列表

顺便说一句,如果您在MVC上下文中,您可以引用System.Web.Routing并使用以下代码段

foreach (var userItem in UserItems)
{
 // RVD is provided by routing framework and it gives a dictionary 
 // of the object property names and values, without us doing 
 // anything funky. 
 var userItemDictionary= new RouteValueDictionary(userItem);
}

这就是你能做到的。(顺便说一句,您的代码可能会在“dictionary key not being unique”(字典键不唯一)上出错,因为第二个userItem将尝试向字典添加相同的属性名。您可能需要一个
列表

顺便说一句,如果您在MVC上下文中,您可以引用System.Web.Routing并使用以下代码段

foreach (var userItem in UserItems)
{
 // RVD is provided by routing framework and it gives a dictionary 
 // of the object property names and values, without us doing 
 // anything funky. 
 var userItemDictionary= new RouteValueDictionary(userItem);
}
试试这个

foreach (var property in theProperties)
{
  var userItemVal = property.GetValue(userItem, null);
  userDetails.Add(property.Name, userItemVal.ToString());
}
试试这个

foreach (var property in theProperties)
{
  var userItemVal = property.GetValue(userItem, null);
  userDetails.Add(property.Name, userItemVal.ToString());
}

添加有关
属性.GetValue
的更多详细信息,您需要将userItem作为第一个参数传递给编译器,以告知需要从哪个引用变量数据中查找。添加有关
属性.GetValue
的更多详细信息,您需要将userItem作为第一个参数传递给编译器,告诉编译器需要从哪个引用变量数据中查找。@B.K.property.GetValue将返回一个对象。我们不能将其直接添加到字典中,因为需要字符串值。因此出现了空检查。请看字典的定义。它需要一个字符串值。如果我遗漏了什么,请告诉我。很高兴纠正我自己的错误。谢谢,这很有效,你对字典的看法是对的。我会用列表代替。酷。如果使用列表,您可能不知道哪个属性对应于哪个userItem。e、 如果有两个userItems,那么你就有两个“Name”属性,不知道它是属于userItem 1还是2。解决方法是像您最初的想法一样使用Dictionary,但在添加键时,将其添加为(property.Name+“[”+index+“]),以便获得用户项的索引。这将为您提供一个字典,其中所有[0]键都属于userItem1,依此类推。@B.K.property.GetValue将返回一个对象。我们不能将其直接添加到字典中,因为需要字符串值。因此出现了空检查。请看字典的定义。它需要一个字符串值。如果我遗漏了什么,请告诉我。很高兴纠正我自己的错误。谢谢,这很有效,你对字典的看法是对的。我会用列表代替。酷。如果使用列表,您可能不知道哪个属性对应于哪个userItem。e、 如果有两个userItems,那么你就有两个“Name”属性,不知道它是属于userItem 1还是2。解决方法是像您最初的想法一样使用Dictionary,但在添加键时,将其添加为(property.Name+“[”+index+“]),以便获得用户项的索引。这将为您提供一个字典,其中所有[0]键都属于userItem1等。任何其他详细信息/解释都会有所帮助。任何其他详细信息/解释都会有所帮助。