C# How to get properties names as strings of generic class using nameof

95 Views Asked by At

C# How to get properties names of generic class using nameof without specifing a type. I have class as follow:

public class Example<T> where T : class
{
    public DateTime CreatedAt { get; set; } = DateTime.Now; 
}

And I want to get "CreateAt" as string using following:

nameof(Example<>.CreatedAt)

but it doesn't work, I need to specify the type:

nameof(Example<int>.CreatedAt)

Although, for checking the type I can use construction:

typeof(BaseEntity<>)

Why it doesn't work with nameof? Why do I need to define the type?

0

There are 0 best solutions below