Get Enum Underlying Primitive Info - CSharp System.Reflection

CSharp examples for System.Reflection:Enum

Description

Get Enum Underlying Primitive Info

Demo Code


using System.Threading.Tasks;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using System.Reflection.Emit;
using System.Reflection;
using System.Linq;
using System.IO;//from   ww  w .  j ava2  s  . co  m
using System.Globalization;
using System.Collections.Generic;
using System;

public class Main{
        static bool GetEnumUnderlyingPrimitiveInfo(Type primitiveType, out bool signed, out int numBytes)
        {
            if (primitiveType == typeof(byte))
            {
                signed = false;
                numBytes = 1;
                return true;
            }

            if (primitiveType == typeof(sbyte))
            {
                signed = true;
                numBytes = 1;
                return true;
            }

            if (primitiveType == typeof(short))
            {
                signed = true;
                numBytes = 2;
                return true;
            }

            if (primitiveType == typeof(ushort))
            {
                signed = false;
                numBytes = 2;
                return true;
            }

            if (primitiveType == typeof(int))
            {
                signed = true;
                numBytes = 4;
                return true;
            }

            if (primitiveType == typeof(uint))
            {
                signed = false;
                numBytes = 4;
                return true;
            }

            if (primitiveType == typeof(long))
            {
                signed = true;
                numBytes = 8;
                return true;
            }

            if (primitiveType == typeof(ulong))
            {
                signed = false;
                numBytes = 8;
                return true;
            }

            signed = false;
            numBytes = -1;

            return false;
        }
}

Related Tutorials