I do have a lot of static classes that represent different states in different modules. However they share common algorithms that extract its information.
public static class Constants
{
public static readonly int A = 0;
}
So for now I have for each class multiple static functions that does this. They only differ in the type of the static class that is processed (and its overall name).
public static SelectListItem getConstantsSelectListItem()
{ // pseudo example
return new SelectListItem { Text = "A" , Value = Constants.A };
}
To remove current and avoid future codebloat, I'd like to use reflection on the static classes. Here's my approach, that would do the job (if it was possible):
public static ReturnType getProperties< T >()
{ // basically same logic as getConstantsSelectListItem
var propertyList = typeof( T ) .GetFields( BindingFlags.Public | BindingFlags.Static ).ToList();
foreach( var item in propertyList )
{
var curConstant = (int)( item.GetValue( null ) );
// do some work here..
}
}
var constantsProperties = getProperties<Constants>();
The error is:
static types cannot be used as argument types
I have read, that for generics only instances (and therefore no static classes) can be used.
What would be a good way to make something similar work?