Asp.net core 如何在Asp.net内核中获取类的属性

Asp.net core 如何在Asp.net内核中获取类的属性,asp.net-core,Asp.net Core,我有一个模型: public class Message { public string CustomerName { get; set; } public DateTime Date { get; set; } public string RegistrationCode { get; set; } } 我想动态地获取它的属性,在以前版本的.Net中,我们通过:obj.GetType().GetProperties()但是在.Net Core中,我们没有.GetPro

我有一个模型:

public class Message
{
    public string CustomerName { get; set; }
    public DateTime Date { get; set; }
    public string RegistrationCode { get; set; }
}

我想动态地获取它的属性,在以前版本的
.Net
中,我们通过:
obj.GetType().GetProperties()
但是在
.Net Core
中,我们没有
.GetProperties()
,我该怎么做呢?

您可能没有添加以下using语句:

using System.Reflection;
我尝试运行以下控制台应用程序(我认为您不会注意到asp.net核心项目中的任何差异):

我没有注意到任何问题。以下是project.json供参考:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

您确定使用最新的.net核心版本吗

使用System.Reflection尝试
obj.GetType().GetTypeInfo().DeclaredProperties
;编写的代码是正确的,不需要额外的包。
{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.1"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}