C # Enum for data types

Is there an enumeration in C # that contains C # types. So I can define a property in a class that accepts a data type (int, string) from the user.

+4
source share
7 answers

Do you just want to associate the enum value with a string? You can use the Description attribute.

 public enum MyEnum { [Description("My first value.")] FirstValue, [Description("My second value.")] SecondValue, [Description("My third value.")] ThirdValue } private string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } } 

Another option to define a mapping would be to use Dictionary<int, string> .

+3
source

The system has TypeCode Enumeration . It looks like it covers all the basic types.

You can get a TypeCode for any object using Type.GetTypeCode ():

 TypeCode typeCode = Type.GetTypeCode(anObject.GetType()); 
+3
source

There is a boolean property Type-type "IsPrimitive", I hope this helps you.

+2
source

There is nothing like this in BCL.

Why do you need this?

+1
source

Based on your editing sounds like generics , but I still doubt why the property would be acceptable to be an int or string. These are really different things that can only lead to an increase.

+1
source

Why do you need this? The property is already a "filter" for what data it can receive.

Take a look:

Property Overloading in C #

0
source

Unfortunately, there is no such enumeration in the structure, you will need to create it manually (as we did, since we had the same need as you).

Sincerely.

0
source

All Articles