C# Getting the Type of a Public Variable based on an Enum value -


I have a class that parses data in a comma-delimited text file. I have an enum for field So that I can help parsing data easily. The square that parses all records, holds public variables for each field, and of course their variable types. I need to get the type of these variables on the basis of the alpha.

  public enum DatabaseField: int {NumID1 = 1, NumID2 = 2, NumID3 = 3,}; Public class databaserecordinfo {public long NumID1 {get; Set; } Public Professor NumID2 {get; Set; } Public Short NumID3 {get; Set; } Public Static Type GetType (DatabaseField Field) {Type Type; Switch (field) {case DatabaseField.NumID1: type = typeof (long); break; Case DatabaseField.NumID2: Type = Type (int); break; Case DatabaseField.NumID3: Type = Type (Small); break; Default: type = typef (int); break; } Return type; }}; NumID1, NumID2, NumID3 all meet within my constructor. However, I want to get these types without ever making an example of  DataBaseRecordInfo . Right now my static method will work above, however, if I want to change the variable type, then I have to convert it to 2 places. Is there any way to get around it to change it in both places and as a stable method? 

If the name is always matching, then you can do this by reflection.

  Return Typefafa (DatabaserecordInfo) .GetProperty (field.ToString (), BindingFlags.Public | BindingFlags.Instance). Property Type;  

You can also cache these values ​​in a dictionary, so if you only meet, return the dictionary entry, otherwise use the reflection and cache the result.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -