How to get name of a class property?

61,655

Solution 1

The result of ClassName.IntProperty is just an integer value. As soon as it's executed and the result is returned, there's no trace of it having come from IntProperty.

If you're using .NET 3.5 you can use an expression tree instead, usually created via a lambda expression:

Expression<Func<int>> exp = () => ClassName.IntProperty;

You can then compile and execute the expression and separately find out what it's doing (retrieving IntProperty in this case). I'm not really sure whether this is suitable for what you want to do though.

If you do work out how to save the property name in the database, then GetProperty is the way to go on the retrieval front.

Perhaps if you could give more context in the question in terms of how you want to use this, we could help more. You've shown just an expression - if you could show it in terms of where you'd be using it, that would be great.

EDIT: You've expanded the property, but not how it's being called. Do you need to call it directly, rather than just fetching the list of properties using Type.GetProperties and storing the list of property names in the database?

Again, if you could show the code which calls the property, and how you want it to interact with the database, we may be able to make more progress.

Solution 2

With C#6.0 you can get it by

nameof(ClassName.IntProperty)

Solution 3

I think that the use of the GetProperty method in this case, is redundant, because you need to know the property name to call the method.

You could loop through your properties and extract its name:

foreach (PropertyInfo p in typeof(ClassName).GetProperties())
{
    string propertyName = p.Name;
    //....
}

Solution 4

Type objectType = this.GetType();
PropertyInfo property = objectType.GetProperty("intProperty");
System.Console.Write(property.Name);

Is this what you need?

Share:
61,655
Jeff
Author by

Jeff

Updated on July 06, 2021

Comments

  • Jeff
    Jeff almost 3 years

    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.