Enum value to string

前端 未结 6 1818
无人共我
无人共我 2021-02-06 21:59

Does anyone know how to get enum values to string?

example:

private static void PullReviews(string action, HttpContext context)
{
    switch (action)
            


        
6条回答
  •  再見小時候
    2021-02-06 22:39

    I'd suggest you go the other way - try to use the actual enum values as much as possible (by converting the string to an enum value as soon as you can), rather than passing strings around your application:

    ProductReviewType actionType = (ProductReviewType)Enum.Parse(typeof(ProductReviewType), val);
    // You might want to add some error handling here.
    PullReviews(actionType);
    
    private static void PullReviews(ProductReviewType action, HttpContext context)
    {
        switch (action)
        {
            case ProductReviewType.Good:
                PullGoodReviews(context);
                break;
            case ProductReviewType.Bad:
                PullBadReviews(context);
                break;
        }
    }
    

    Note that I've changed your method signature to accept a ProductReviewType argument; this makes it clear what your method actually needs to implement its logic, and fits with my original point that you should try as much as possible not to pass strings around.

提交回复
热议问题