简体   繁体   中英

Getting class property name in C#

I want to write common function which get all or single class property name

public class MyClass
{
        public int TemplateId { get; set; }
        public int TemplateCategoryId { get; set; }
}

Example: In other method I need to call

var propertyName = MyClass.TemplateId.GetPrertyName(); // will Return TemplateId 

How can I make it?

Using the c# 6 nameof operator, you can write:

nameof(MyClass.TemplateId);

More details can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM