3

Please consider this class:

public static class Age
{    
    public static readonly string F1 = "18-25";
    public static readonly string F2 = "26-35";
    public static readonly string F3 = "36-45";
    public static readonly string F4 = "46-55";
}

I wanto to write a function that get "F1" and return "18-25".for example

private string GetValue(string PropertyName)
....

How can I do it?

JYelton
  • 35,664
  • 27
  • 132
  • 191
Arian
  • 12,793
  • 66
  • 176
  • 300

5 Answers5

11

You can simply use SWITCH statement to perform above task:

public static string GetValue(string PropertyName)
{
    switch (PropertyName)
    {
        case "F1":
            return Age.F1;
        case "F2":
            return Age.F2;
        case "F3":
            return Age.F3;
        case "F4":
            return Age.F4;
        default:
            return string.Empty;
    }
}

Using Reflection, you can do like this:

public static string GetValueUsingReflection(string propertyName)
{
    var field = typeof(Age).GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
    var fieldValue = field != null ? (string)field.GetValue(null) : string.Empty;
    return fieldValue;
}
Akash KC
  • 16,057
  • 6
  • 39
  • 59
5

I did some testing, for this case this will work:

public static string GetValue(string PropertyName)
{
   return typeof(Age).GetField(PropertyName).GetValue(typeof(Age));
}

It seems static constants work a little different. But the above worked with the class in the OQ.

For a more general case, see this question.


This is how it is done with reflection:

public static string GetValue(string PropertyName)
{
   return Age.GetType().GetProperty(PropertyName).ToString();
}

Note, GetProperty() can return null, which would crash if you passed in "F9999"

I haven't tested, you might need this:

public static string GetValue(string PropertyName)
{
   return Age.GetType().GetProperty(PropertyName,BindingFlags.Static).ToString();
}

General case as the comment:

public static string GetValue(object obj, string PropertyName)
{
   return obj.GetType().GetProperty(PropertyName,BindingFlags.Static).ToString();
}
Community
  • 1
  • 1
Hogan
  • 69,564
  • 10
  • 76
  • 117
1

Use Reflection with Linq:

 private string GetValue(string propertyName)
 {
       return typeof(Age).GetFields()
           .Where(field => field.Name.Equals(propertyName))
           .Select(field => field.GetValue(null) as string)
           .SingleOrDefault();
 }
cuongle
  • 74,024
  • 28
  • 151
  • 206
0

you should use the class Type. you could get the one for the class you are using withe the getType() function. after you have the type use GetProperty function. you would get a propertyinfo class. this class has a getValue function. this value will return the value of the property.

elyashiv
  • 3,623
  • 2
  • 29
  • 52
0

try this and enjoy :

public static string GetValueUsingReflection(string propertyName)
    {
        var field = Type.GetType("Agenamespace" + "." + "Age").GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
        var fieldValue = field != null ? (string)field.GetValue(null) : string.Empty;
        return fieldValue;
    }

the Agenamespace is namespace that the Age class is declared in.

Habib Zare
  • 1,206
  • 8
  • 17