I want to display all of the private property.
static void Main(string[] args)
{
var user = new User(5, "cash");
foreach(var prop in typeof(User).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic))
{
if (prop.GetSetMethod().IsPrivate) //this line goes wrong
Console.WriteLine("Private {0} = {1}", prop.Name, prop.GetValue(user, null));
}
Console.ReadLine();
}
public class User
{
public int Id { get ; }
public string Name { get; set; }
private string identify { get { return Id.ToString() + Name; } }
public User(int id,string name)
{
this.Id = id;
Name = name;
}
}
Exactly ouput:throw exception System.NullReferenceException
Expected output: Private number=3