简体   繁体   English

在C#中获取类属性名称

[英]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: 使用c#6 nameof运算符,您可以编写:

nameof(MyClass.TemplateId);

More details can be found here 可以在这里找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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