Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7080425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:47:38+00:00 2026-05-28T06:47:38+00:00

static void Main(string[] args) { List<string> tests = new List<string>() { TypeTester.NonExist, TypeTester.Thing, TypeTester.Fruit,

  • 0
 static void Main(string[] args)
    {
        List<string> tests = new List<string>() { "TypeTester.NonExist", "TypeTester.Thing", "TypeTester.Fruit", "TypeTester.Color" };
        PrintEnums();
        foreach (var item in tests)
        {
            try
            {
                ConvertFromTypeName(item);
            }
            catch (TypeLoadException)
            {
                Console.WriteLine("Could not load {0}", item);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }    
        }
        Console.ReadLine();
    }

    static void PrintEnums()
    {
        Console.WriteLine("Printing Fruit");
        foreach (Fruit thing in Enum.GetValues(typeof(Fruit)))
        {
            Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription());
        }
        Console.WriteLine("Printing Color");
        foreach (Color thing in Enum.GetValues(typeof(Color)))
        {
            Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription());
        }
    }

    static void ConvertFromTypeName(string typeName)
    {
        Type type = Type.GetType(typeName, true);
        if (!type.IsEnum)
            throw new ArgumentException(typeName + " is not an enum.");

        Console.WriteLine("UnderlingType: {0}", type.UnderlyingSystemType);
        string description = string.Empty;
        foreach ( var thing in Enum.GetValues((type.UnderlyingSystemType)))
            {
//This will not compile                
//Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.GetDescription());
                Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.ToString());
        }
    }
}

public class Thing
{
    public int Id { get; set; }
    public string MyName { get; set; }
}

public enum Fruit
{
    [System.ComponentModel.DescriptionAttribute("I am an apple")]
    Apple = 8,
    [System.ComponentModel.DescriptionAttribute("I am an Banana")]
    Banana,
    [System.ComponentModel.DescriptionAttribute("I am an Grape")]
    Grape,
    [System.ComponentModel.DescriptionAttribute("I am an Kiwi")]
    Kiwi,
    [System.ComponentModel.DescriptionAttribute("I am an Orange")]
    Orange
}

public enum Color
{
    [System.ComponentModel.DescriptionAttribute("I am the color Red")]
    Red = 56,
    [System.ComponentModel.DescriptionAttribute("I am the color Green")]
    Green,
    [System.ComponentModel.DescriptionAttribute("I am the color Blue")]
    Blue,
    [System.ComponentModel.DescriptionAttribute("I am the color Black")]
    Black,
    [System.ComponentModel.DescriptionAttribute("I am the color White")]
    White
}

public static class Helper
{
    public static string GetDescription(this Enum value)
    {
        System.Reflection.FieldInfo field = value.GetType().GetField(value.ToString());
        System.ComponentModel.DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(System.ComponentModel.DescriptionAttribute)) as System.ComponentModel.DescriptionAttribute;
        return attribute == null ? value.ToString() : attribute.Description;
    }
}

Why in ConvertFromTypeName if the underlying type is Fruit or Color I cannot call the GetDescription extension method? It seems to not to be able to infer that from the var.
I would like this to work just like PrintEnums, the GetDescription extension.

Printing Fruit
Value: 8, Description: I am an apple
Value: 9, Description: I am an Banana
Value: 10, Description: I am an Grape
Value: 11, Description: I am an Kiwi
Value: 12, Description: I am an Orange
Printing Color
Value: 56, Description: I am the color Red
Value: 57, Description: I am the color Green
Value: 58, Description: I am the color Blue
Value: 59, Description: I am the color Black
Value: 60, Description: I am the color White
Could not load TypeTester.NonExist
TypeTester.Thing is not an enum.
UnderlingType: TypeTester.Fruit
Value: 8, Description: Apple
Value: 9, Description: Banana
Value: 10, Description: Grape
Value: 11, Description: Kiwi
Value: 12, Description: Orange
UnderlingType: TypeTester.Color
Value: 56, Description: Red
Value: 57, Description: Green
Value: 58, Description: Blue
Value: 59, Description: Black
Value: 60, Description: White
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:47:38+00:00Added an answer on May 28, 2026 at 6:47 am

    Got it. In the foreach of ConvertFromTypeName method I changed

    Console.WriteLine("Value: {0}, Description: {1}", (int)thing, thing.ToString());
    

    to

    Console.WriteLine("Value: {0}, Description: {1}", (int)thing, ((Enum)thing).GetDescription());
    

    Looks like all I needed to do was cast thing to an Enum to get @ the extension method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
considering this example: public static void main(final String[] args) { final List<String> myList =
static void Main(string[] args) { //read in the file StreamReader convert = new StreamReader(../../convert.txt);
import java.util.*; public class Test { public static void main(String[] args) { List db
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
This code compiles: private static void Main(string[] args) { bool? fred = true; if
class Program { static void Main(string[] args) { String value = Two; Type enumType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.