How to get name and value constant in class with Net Core

I will show you a small code how to get value and name in constant



public class Vendors
    {
public const int VendorA = 1;
public const int VendorB = 2;
}

 foreach (FieldInfo field in typeof(Vendors).GetFields())
{

 int venId = 0;                     
 int.TryParse(field.GetRawConstantValue().ToString(), out venId); // return Vendor value is 2 and 2
 string vendorName = field.Name; // return vendor name VendorA and VendorN

}

Reactions

Post a Comment

0 Comments