views:

691

answers:

3

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 dyanmically save property name string into database, and later on retrive from 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 database. Later on the code will read the string and dynamically create the task and pass it into another to execute.

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

Is this what you need?

sorry it's not what i wanted. i've edited the post to make it more clear.
Jeffrey C
+3  A: 

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;
    //....
}
CMS
+1  A: 

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.

Jon Skeet
See also the discussion here (and other places): http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression/
Marc Gravell