C# Get Class Property Name – How to Use Reflection

c++gettypepropertiesreflection

Is there anyway I can get the name of class property IntProperty?

public class ClassName
{
  public static int IntProperty { get { return 0; } }
}

//something like below but I want to get the string of "IntProperty"
ClassName.IntProperty.GetType().Name

Basically what I want to do is to dynamically save property name string into the database, and later on retrieve it from the database and invoke the property dynamically.

Seems like what I am looking for is similar to duck typing I think.

Thanks!

UPDATED:

This is the actual code. This is more like a workflow kind of thing. But each task is defined as property of a class (class is used to group tasks).

public class ApplicationTask
{
    public static Task<string> SendIncompleteNotification
    {
        get
        {
            return new Task<string>
                (
                a => Console.WriteLine("Sample Task")
                , "This is a sample task which does nothing."
                );
        }
    }
}

So, the code will be able to retrieve the full name of the class and property something like: namespace.ApplicationTask.SendIncompleteNotification and save this into the database. Later on, the code will read the string and dynamically create the task and pass it into another to execute.

Best Answer

With C#6.0 you can get it by

nameof(ClassName.IntProperty)
Related Question