次のコード (StackOverflow にあります) が機能します。
object ob = new { Name = "Erwin Okken", Age = 23, Position = new Point(2, 5) };
Type type = ob.GetType();
PropertyInfo pr = type.GetProperty("Name");
string value = pr.GetValue(ob, null).ToString(); // Erwin Okken
ただし、独自のクラスを使用すると機能しません。
public class Subject
{
public string Name;
public int Age;
public Point Position;
public string Stringtest;
public int IntTest;
public Subject()
{
}
}
Type type = ob.GetType();
PropertyInfo pr = type.GetProperty("Name"); // null
string value = pr.GetValue(ob, null).ToString();
すべての Bindingflags を試しましたが、変数 "pr" は null のままです。誰にもアイデアがありますか?