将类对象转换为类索引

Class Person {
  int Id 
  string Name 
  string Address 
  // etc 
}

而不是像Person.Id,Person.Name,Person.Address那样访问它。我想像Person ['Id'],Person ['Name']一样通过索引访问它。是否有任何代码生成或linq转换。

评论
  • uut
    uut 回复

    You can use Json.NET's JObject class

    Person p = new Person() { Id = 1, Address = "A", Name = "B" };
    var obj = JObject.FromObject(p);
    Console.WriteLine(obj["Id"]); //1