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. Assembly assembly = Assembly.Load(assemblyFullName);
  6. IEnumerable<TypeInfo> primitiveTypes =
  7. assembly.DefinedTypes.Where(definedType => definedType.IsPrimitive && definedType != typeof(IntPtr) && definedType != typeof(UIntPtr));
  8. int totalPrimitiveTypes = primitiveTypes.Count();
  9. string[] dataPrimitiveTypes = new string[totalPrimitiveTypes];
  10. using (var provider = new CSharpCodeProvider())
  11. {
  12. dataPrimitiveTypes = primitiveTypes.Select(x => provider.GetTypeOutput(new CodeTypeReference(x))).ToArray();
  13. }
  14. this._output.WriteLine(StringOutputHelper.WriteString(dataPrimitiveTypes, '•'));
  15. Assert.True(totalPrimitiveTypes == 12);
  16. }
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. Assembly assembly = Assembly.Load(assemblyFullName);
  7. IEnumerable<TypeInfo> primitiveTypes =
  8. assembly.DefinedTypes.Where(definedType => definedType.IsPrimitive && definedType != typeof(IntPtr) && definedType != typeof(UIntPtr));
  9. var attr = BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static;
  10. var typeInformation = new List<TypeInformation> { };
  11. using (var provider = new CSharpCodeProvider())
  12. {
  13. foreach (var primitiveType in primitiveTypes)
  14. {
  15. switch (primitiveType.FullName)
  16. {
  17. case "System.SByte":
  18. case "System.Int16":
  19. case "System.Int32":
  20. case "System.Int64":
  21. case "System.Byte":
  22. case "System.UInt16":
  23. case "System.UInt32":
  24. case "System.UInt64":
  25. typeInformation.Add(new TypeInformation
  26. {
  27. Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
  28. DotNetType = primitiveType.FullName,
  29. MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),
  30. MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),
  31. TypeInformationCategory = TypeInformationCategory.IntegerPrimitiveTypes
  32. });
  33. break;
  34. case "System.Single":
  35. case "System.Double":
  36. case "System.Decimal":
  37. typeInformation.Add(new TypeInformation
  38. {
  39. Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
  40. DotNetType = primitiveType.FullName,
  41. MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),
  42. MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),
  43. TypeInformationCategory = TypeInformationCategory.FloatingPrimitiveTypes
  44. });
  45. break;
  46. case "System.Boolean":
  47. typeInformation.Add(new TypeInformation
  48. {
  49. Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
  50. DotNetType = primitiveType.FullName,
  51. MinValue = (primitiveType.GetField("FalseString", attr)).GetValue(primitiveType).ToString(),
  52. MaxValue = (primitiveType.GetField("TrueString", attr)).GetValue(primitiveType).ToString(),
  53. TypeInformationCategory = TypeInformationCategory.FloatingPrimitiveTypes
  54. });
  55. break;
  56. case "System.Char":
  57. typeInformation.Add(new TypeInformation
  58. {
  59. Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
  60. DotNetType = primitiveType.FullName,
  61. MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),
  62. MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),
  63. TypeInformationCategory = TypeInformationCategory.CharacterPrimitiveTypes
  64. });
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. }
  71. public struct TypeInformation
  72. {
  73. public string Alias_Name { get; set; }
  74. public string DotNetType { get; set; }
  75. public string MinValue { get; set; }
  76. public string MaxValue { get; set; }
  77. public TypeInformationCategory TypeInformationCategory { get; set; }
  78. public override string ToString()
  79. {
  80. return string.Format("Name Or Alias: {0}{1}.NET Type: {2}{3}Min Value : {4}{5}Max Value: {6}{7}",
  81. this.Alias_Name,
  82. Environment.NewLine,
  83. this.DotNetType,
  84. Environment.NewLine,
  85. this.MinValue,
  86. Environment.NewLine,
  87. this.MaxValue,
  88. Environment.NewLine);
  89. }
  90. public static string GetOutput(List<TypeInformation> typeInformations)
  91. {
  92. StringBuilder output = new StringBuilder();
  93. foreach (var item in typeInformations)
  94. {
  95. output.AppendLine(item.ToString());
  96. }
  97. return output.ToString();
  98. }
  99. }
  100. [Flags]
  101. public enum TypeInformationCategory : byte
  102. {
  103. IntegerPrimitiveTypes,
  104. FloatingPrimitiveTypes,
  105. CharacterPrimitiveTypes,
  106. BooleanPrimitiveTypes
  107. }
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!