C# 从PropertyInfo获取属性值

C# 从PropertyInfo获取属性值,c#,C#,我有以下课程: public class MagicMetadata { public string DataLookupField { get; set; } public string DataLookupTable { get; set; } public List<string> Tags { get; set; } } 因此,我正在尝试这样的事情,根据 我不明白的是,我是如何得到我正在循环的属性的值的。我一点也不懂这句话。为什么我需要将其传递给对象?什么东西

我有以下课程:

public class MagicMetadata
{
  public string DataLookupField { get; set; }
  public string DataLookupTable { get; set; }
  public List<string> Tags { get; set; }
}
因此,我正在尝试这样的事情,根据

我不明白的是,我是如何得到我正在循环的属性的值的。我一点也不懂这句话。为什么我需要将其传递给对象?什么东西

另外,我在这里查看了现有的答案,但没有找到明确的答案。

更新至:

foreach (PropertyInfo propertyInfo in md.GetType().GetProperties())
{
   new FormMetaData
   {
     FormFieldName = propertyInfo.Name,
     MetadataLabel = propertyInfo.GetValue(md) // <--
   }
}

在反思的同时,您需要通过和方法分别访问这些成员。

嗯,我想我现在明白了-比我想象的要简单得多,但没有为我单击。非常感谢。但是,对于类型为
List
Tags
属性的值,这将如何计算?OP到底需要显示什么作为他们的MetadataLabel?@dlatikay我理解这种担心,但一旦我获得了属性,我就可以找到一种处理类型的方法。我主要关心的是通过反思来获得信息。剩下的我等会再弄清楚。“到底是什么?”。。它清楚地表明要传递对象实例。为什么要传递字符串
PropertyInfo
类保存有关属性的元数据。如果您告诉它应该从哪个对象获取值,它可以帮助您获取值。您需要传递的对象必须是一个有效的实例。@Greggz Yea,“根本”,它最初没有点击这基本上等同于javascript的
someObject['somePropName']
@Greggz Yea,close,不过,我猜在该示例中不需要第二个
null
参数。我可能会删除这个。编辑:一旦有答案,就不能删除。@VSO这可能是一个不推荐使用的功能。然而,它们指的是同一个问题,有着相同的解决方案。最好暂时不谈,你还是会因此得到分数的
public class FormMetadataItem 
{
  public string FormFieldName { get; set; }
  public string MetadataLabel { get; set; }
}
foreach (PropertyInfo propertyInfo in md.GetType().GetProperties())
{
   new FormMetaData
   {
     FormFieldName = propertyInfo.Name,
     MetadataLabel = propertyInfo.GetValue(metadata.Name) //This doesn't work
   }
}
foreach (PropertyInfo propertyInfo in md.GetType().GetProperties())
{
   new FormMetaData
   {
     FormFieldName = propertyInfo.Name,
     MetadataLabel = propertyInfo.GetValue(md) // <--
   }
}
class MyClass {
    string MyProperty {get; set;} // This is a property
    string MyField; // This is a field
}