Using PropertyInfo.GetValue()

前端 未结 1 1945
花落未央
花落未央 2020-12-08 21:05

I have a class that creates a static array of all properties, using a static constructor. I also have a function -- GetNamesAndTypes() -- that lists the name & type of

相关标签:
1条回答
  • 2020-12-08 21:58

    In your example propertyInfo.GetValue(this, null) should work. Consider altering GetNamesAndTypesAndValues() as follows:

    public void GetNamesAndTypesAndValues()
    {
      foreach (PropertyInfo propertyInfo in allClassProperties)
      {
        Console.WriteLine("{0} [type = {1}] [value = {2}]",
          propertyInfo.Name,
          propertyInfo.PropertyType,
          propertyInfo.GetValue(this, null));
      }
    }
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题