Monday, 2 December 2024

How to Create a Generic Method to Get the Enum Description in C#

        public static SelectList GetSelectListForEnumWithDescription(Type enumType)
        {
            var list = new List<Object>();
            Array enumValues = Enum.GetValues(enumType);
         
            foreach (Enum enumValue in enumValues)
            {
                list.Add(new
                              {
                                 ID = (int)Enum.Parse(enumType, enumValue.ToString()),
                                 Value = GetEnumDescription(enumValue)});
                               }
              return new SelectList(list, "ID", "Value");
        }

        public static string GetEnumDescription(Enum value)
        {

            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[]) fi.GetCustomAttributes(typeof (DescriptionAttribute), false);

            if (attributes != null && attributes.Length > 0)

                return attributes[0].Description;

            else return value.ToString();
        }
    }

Developer and Reviewer Checklist - Keep Handy while Developing Code

  1. Does the code work as expected? 
  2. Does the code follow the StyleCop/FxCop/JSLint/SonarLint rules?
  3. Review static analysis warning suppressions if any.
  4. Do you see any code duplication?
  5. Please refer the Portal Standardization sheet as well while implementation
  6. Is there any copy-pasted code from any other source?
  7. Does the code follow a modular structure?
  8. Validate code metrics.
  9. Is there any code that is commented out?
  10. Are there any TODO markers or any incomplete code?
  11. Is proper authentication and authorization in place? Validate across personas
  12. Adequate logging (Verbose, Warning, Error etc.)?
  13. Does the code handle all exception scenarios?
  14. · What happens if call to external system fail?
  15. · What happens if the call to database fails?
  16. Can the number of database queries be optimized?
  17. Proper use of cache?
  18. Review transactions
  19. Do methods and functions validate inputs before using it?
  20. Are comments present and are they appropriate?
  21. Do loops have proper terminating conditions?
  22. Do methods have too many parameters?
  23. Do methods, classes etc. have meaningful names?
  24. Does the code perform excessive string concatenation?
  25. For custom exception classes, does the code implement the standard exception pattern?
  26. For classes that need to implement, do they implement the standard dispose pattern?
  27. Does the code use dynamic SQL?
  28. Does the code implement guidelines to avoid XSS attack
  29. Does the code implement technique to avoid CSRF attack
  30. Each UI control should have corresponding ID for automation purpose.
  31. For a control type(ex : dropdown), across the pages it should have same control properties.
  32. TFS task status update
  33. Link TFS ID with check-in
  34. Deployment Scripts, RM updated if applicable
  35. Verified the successful deployment in Dev integration environment