Enum value to string

前端 未结 6 1819
无人共我
无人共我 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:59

    This code is gonna works.

    private enum ProductReviewType{good, bad};
    
    private static void PullReviews(string action)
    {
        string goodAction = Enum.GetName(typeof(ProductReviewType), ProductReviewType.good);
        string badAction = Enum.GetName(typeof(ProductReviewType), ProductReviewType.bad);
        if (action == goodAction)
        {
            PullGoodReviews();
        }
        else if (action == badAction)
        {
            PullBadReviews();
        }
    }
    
    public static void PullGoodReviews()
    {
        Console.WriteLine("GOOD Review!");
    }
    
    public static void PullBadReviews()
    {
        Console.WriteLine("BAD Review...");
    }
    

    Since the parsed string is not constant it can not be used by Switch statement. Compiled in VS2005

    You can use another temp variable to save the Type of enum class, this may improve performance.

提交回复
热议问题