Extract C# Primitive Types Using Reflection

Introduction

 
In this blog post, we are going to extract to C# primitive types with the help of the System.Reflection. Using it, you’ll see how we can extract those types.
 

Data Type in C#

 
The C# language offers primitive (also known as predefined) types and non-primitive (also known as user-defined) types. 
 
Primitive or Predefined Types Non-Primitive or User-Defined Types
bool class
byte struct
char enum
double interface
short delegate
int array
long  
sbyte  
float  
ushort  
uint  
ulong  
 
See the example below on how the primitive types were extracted from the System.Private.CoreLib.
  1. [Fact]  
  2. public void UnitTest_GetAll_PrimitiveTypes()  
  3. {  
  4.     string assemblyFullName = "System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";  
  5.   
  6.     Assembly assembly = Assembly.Load(assemblyFullName);  
  7.   
  8.     IEnumerable<TypeInfo> primitiveTypes =  
  9.         assembly.DefinedTypes.Where(definedType => definedType.IsPrimitive && definedType != typeof(IntPtr) && definedType != typeof(UIntPtr));  
  10.   
  11.     int totalPrimitiveTypes = primitiveTypes.Count();  
  12.   
  13.     string[] dataPrimitiveTypes = new string[totalPrimitiveTypes];  
  14.   
  15.     using (var provider = new CSharpCodeProvider())  
  16.     {  
  17.         dataPrimitiveTypes = primitiveTypes.Select(x => provider.GetTypeOutput(new CodeTypeReference(x))).ToArray();  
  18.     }  
  19.     this._output.WriteLine(StringOutputHelper.WriteString(dataPrimitiveTypes, '•'));  
  20.   
  21.     Assert.True(totalPrimitiveTypes == 12);  
  22. }  
The above sample code shows you how to extract the primitive types in a bulleted list, almost similar to the above table in the first column. You can also gather the name or alias, .NET type, and its maximum and minimum value. See the example below. 
  1. /* The helper class TypeInformation is shown below after this example [UnitTest_GetAll_PrimitiveTypes_With_Information].*/  
  2. [Fact]  
  3. public void UnitTest_GetAll_PrimitiveTypes_With_Information()  
  4. {  
  5.     string assemblyFullName = "System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";  
  6.   
  7.     Assembly assembly = Assembly.Load(assemblyFullName);  
  8.   
  9.     IEnumerable<TypeInfo> primitiveTypes =  
  10.         assembly.DefinedTypes.Where(definedType => definedType.IsPrimitive && definedType != typeof(IntPtr) && definedType != typeof(UIntPtr));  
  11.   
  12.     var attr = BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static;  
  13.   
  14.     var typeInformation = new List<TypeInformation> { };  
  15.   
  16.     using (var provider = new CSharpCodeProvider())  
  17.     {  
  18.         foreach (var primitiveType in primitiveTypes)  
  19.         {  
  20.             switch (primitiveType.FullName)  
  21.             {  
  22.                 case "System.SByte":  
  23.                 case "System.Int16":  
  24.                 case "System.Int32":  
  25.                 case "System.Int64":  
  26.                 case "System.Byte":  
  27.                 case "System.UInt16":  
  28.                 case "System.UInt32":  
  29.                 case "System.UInt64":  
  30.                     typeInformation.Add(new TypeInformation  
  31.                     {  
  32.                         Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),  
  33.                         DotNetType = primitiveType.FullName,  
  34.                         MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),  
  35.                         MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),  
  36.                         TypeInformationCategory = TypeInformationCategory.IntegerPrimitiveTypes  
  37.                     });  
  38.                     break;  
  39.                 case "System.Single":  
  40.                 case "System.Double":  
  41.                 case "System.Decimal":  
  42.                     typeInformation.Add(new TypeInformation  
  43.                     {  
  44.                         Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),  
  45.                         DotNetType = primitiveType.FullName,  
  46.                         MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),  
  47.                         MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),  
  48.                         TypeInformationCategory = TypeInformationCategory.FloatingPrimitiveTypes  
  49.                     });  
  50.                     break;  
  51.                 case "System.Boolean":  
  52.                     typeInformation.Add(new TypeInformation  
  53.                     {  
  54.                         Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),  
  55.                         DotNetType = primitiveType.FullName,  
  56.                         MinValue = (primitiveType.GetField("FalseString", attr)).GetValue(primitiveType).ToString(),  
  57.                         MaxValue = (primitiveType.GetField("TrueString", attr)).GetValue(primitiveType).ToString(),  
  58.                         TypeInformationCategory = TypeInformationCategory.FloatingPrimitiveTypes  
  59.                     });  
  60.                     break;  
  61.                 case "System.Char":  
  62.                     typeInformation.Add(new TypeInformation  
  63.                     {  
  64.                         Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),  
  65.                         DotNetType = primitiveType.FullName,  
  66.                         MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),  
  67.                         MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),  
  68.                         TypeInformationCategory = TypeInformationCategory.CharacterPrimitiveTypes  
  69.                     });  
  70.                     break;  
  71.                 default:  
  72.                     break;  
  73.             }  
  74.         }  
  75. }  
  76.   
  77. public struct TypeInformation  
  78. {  
  79.     public string Alias_Name { getset; }  
  80.     public string DotNetType { getset; }  
  81.     public string MinValue { getset; }  
  82.     public string MaxValue { getset; }  
  83.     public TypeInformationCategory TypeInformationCategory { getset; }  
  84.   
  85.     public override string ToString()  
  86.     {  
  87.         return string.Format("Name Or Alias: {0}{1}.NET Type: {2}{3}Min Value : {4}{5}Max Value: {6}{7}",  
  88.                                 this.Alias_Name,  
  89.                                 Environment.NewLine,  
  90.                                 this.DotNetType,  
  91.                                 Environment.NewLine,  
  92.                                 this.MinValue,  
  93.   
  94.                                 Environment.NewLine,  
  95.                                 this.MaxValue,  
  96.                                 Environment.NewLine);  
  97.     }  
  98.   
  99.     public static string GetOutput(List<TypeInformation> typeInformations)  
  100.     {  
  101.         StringBuilder output = new StringBuilder();  
  102.   
  103.         foreach (var item in typeInformations)  
  104.         {  
  105.             output.AppendLine(item.ToString());  
  106.         }  
  107.   
  108.         return output.ToString();  
  109.     }  
  110. }  
  111.   
  112. [Flags]  
  113. public enum TypeInformationCategory : byte  
  114. {  
  115.     IntegerPrimitiveTypes,  
  116.     FloatingPrimitiveTypes,  
  117.     CharacterPrimitiveTypes,  
  118.     BooleanPrimitiveTypes  
  119. }  
Now, the code sample above will output identical to the table below.
 
Name or Alias .NET Type Min Value Max Value
bool System.Boolean FALSE TRUE
byte System.Byte 0 255
char System.Char \0 \xffff
double System.Double -1.7976931348623157E+308 1.7976931348623157E+308
short System.Int16 -32768 32767
int System.Int32 -2147483648 2147483647
long System.Int64 -9223372036854770000 9223372036854770000
sbyte System.SByte -128 127
float System.Single -3.4028235E+38 3.4028235E+38
ushort System.UInt16 0 65535
uint System.UInt32 0 4294967295
ulong System.UInt64 0 18446744073709500000
 

Summary

 
In this blog post, we have discussed how we can extract primitive types via System.Reflection. I hope you have enjoyed this blog post, as I have enjoyed writing and coding the examples. You can also find the sample here at GitHub. Until next time, happy programming!