1
public class Constants
{
    public const string test1 = "This is testvalue1;"
    public const string test2 = "This is testvalue1;"
    public const string test3 = "This is testvalue1;"
    public const string test4 = "This is testvalue1;"
    public const string test5 = "This is testvalue1;"
}

is it in some way possible to get a constants string by usage of var propertyString = Constants.Where(<Propertyname> == test1).ToString() ?

Theun Arbeider
  • 5,259
  • 11
  • 45
  • 68

7 Answers7

4

You will have to do this through reflection.

string fieldName = "test1";
object fieldValue = typeof(Constants).GetField(fieldName, BindingFlags.Static).GetValue(null);
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

Alternatively to the other reflection oriented answers, you could use a dictionary:

public string GetConstantValue(string key)
{
  return _constants[key];
}

private Dictionary<string, string> Constants = new Dictionary<string, string>()
{
  { "test1", "This is testvalue1;" },
  { "test2", "This is testvalue2;" },
  { "test3", "This is testvalue3;" },
  { "test4", "This is testvalue4;" },
  { "test5", "This is testvalue5;" },
};

And then use MyClass.GetConstantValue("test1") to get the constants value.

vc 74
  • 37,131
  • 7
  • 73
  • 89
2

This also could work:

    foreach (var prop in typeof(Constants).GetFields())
    {
        string test = prop.GetRawConstantValue().ToString();

        if (test == "test1")
            MessageBox.Show("You got me!");
    }
dg90
  • 1,243
  • 3
  • 17
  • 30
1

You can use Reflection.

List<string> messages = new List<string>();
foreach (FieldInfo field in typeof(Constants).GetFields().Where(f =>         f.Name.StartsWith("test")))
{
   messages.Add(field.GetRawConstantValue().ToString());

}
Raj Ranjhan
  • 3,869
  • 2
  • 19
  • 29
1

In order to get this,

var p = Constants.Get("test1");//gives 'This is testvalue1'

You can do this -

public class Constants
{
    public const string test1 = "This is testvalue1";
    public const string test2 = "This is testvalue1";
    public const string test3 = "This is testvalue1";
    public const string test4 = "This is testvalue1";
    public const string test5 = "This is testvalue1";

    public static string Get(string propertyName)
    {
      var value = (string)(typeof(Constants).GetField(propertyName,BindingFlags.Static | BindingFlags.Public).GetValue(null));
      return value;
    }
}
ilias
  • 2,620
  • 2
  • 27
  • 37
0

Sure it's possible. Following piece of code uses reflection to loop through all constants of a class.

internal class Program
{
    public const string ConstExample = "constant value";

    private static void Main(string[] args)
    {
        FieldInfo[] fieldInfos = typeof(Program).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

        var constants = fieldInfos.Where(z => z.IsLiteral && !z.IsInitOnly).ToList();
        foreach (var constant in constants)
        {
            string constantName = constant.Name;
            object constantValue = constant.GetValue(null);
            Console.WriteLine(string.Format("Constante name: {0}, constant value: {1}", constantName, constantValue));
        }

        Console.ReadKey();
    }
}
ken2k
  • 48,145
  • 10
  • 116
  • 176
0

For good performance and readability I would recommend an array and an enumeration.

class App
{
    private enum ConstantsEnum: int
    {
      DefaultName = 0,
      DefaultMode = 1,
      DefaultStatus = 2
    }
    private readonly string[] Constants = new string[]{
        "MyProgram",
        "Test",
        "Enabled" };

    private void DoWork()
    {
        Console.WriteLine(Constants[ConstantsEnum.DefaultName]);
    }
}

Note that all variables have to be of type string. If you want to mix and match datatypes then you could possibly use object[] instead of string[], but this would be more expensive and not type-safe.

If you are just looking for a way to group some properties, then this is much better than reflection.

Serge
  • 3,986
  • 2
  • 17
  • 37