I have a class with constants. I have a string that can be the same as the name of one of these constants or not.
Thus, a class with constants ConstClass
has some public const
likeconst1, const2, const3...
public static class ConstClass
{
public const string Const1 = "Const1";
public const string Const2 = "Const2";
public const string Const3 = "Const3";
}
To check if the class contains const
by name, I tried the following:
var field = (typeof (ConstClass)).GetField(customStr);
if (field != null){
return field.GetValue(obj)
}
I don’t know if this is really the right way to do this, but now I don’t know how to get the value, because the method .GetValue
needs obj type ConstClass
(ConstClass is static)
source
share