Quantcast
Channel: How to get a PropertyInfo without property name?
Viewing all articles
Browse latest Browse all 3

How to get a PropertyInfo without property name?

$
0
0

Hi,

We have a simple class 'Person' below.

Code Block

public  class Person

{

public string FirstName(){...}

public int Age() {...}

public string Address() {...}

}

 

 

We can get the name of  the Person class at runtime by

Code Block

Type type = typeof(Person);

string clsName = type.Name;

Console.writeline(clsName);

 

 

If we want to get the Age property name at runtime, we should get its PropertyInfo first. The code looks like

Code Block

Type type = typeof(Person);

PropertyInfo prop = type.GetProperty("Age");

string ageName = prop.Name;

Console.writeline(propName);

 

 

It is strange that we want the name but we should pass the name string in first.

 

So, is it possible that we can get the name of Age property without having to pass a string in?

 

 

 

Any help is appreciated.

Zach@Shine


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images