-1

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?

Mel
  • 5,837
  • 10
  • 37
  • 42
TuanDPH
  • 461
  • 5
  • 14
  • Possible duplicate of [Retrieving Property name from lambda expression](https://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression) – Euphoric Dec 01 '17 at 08:00
  • I've searched and read through it. But I dont want to using Lambda expression because it is hard to read and understand. I think that i will use nameof function. – TuanDPH Dec 01 '17 at 08:06
  • Sorry. This is only possible way if you want to pass it into function. Also, it is not hard to read. Lambdas are common C# element. – Euphoric Dec 01 '17 at 08:08

1 Answers1

5

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

nameof(MyClass.TemplateId);

More details can be found here

KnowHoper
  • 4,352
  • 3
  • 39
  • 54
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396