Is there any way to read the const names into a list?
Example Code
public static class Cars
{
public const int Length= 10;
public const int Height= 15;
public const int Weight = 20;
}
Is there any way to read the const names into a list?
Example Code
public static class Cars
{
public const int Length= 10;
public const int Height= 15;
public const int Weight = 20;
}
You can use reflection like this.
var fields = typeof(Cars)
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(f => new { Name = f.Name, Value = f.GetValue(null) })
.ToList();
Yes:
typeof(Cars).GetFields(BindingFlags.Instance |BindingFlags.Static | BindingFlags.Public).Select(m => m.Name)