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.
- [Fact]
- public void UnitTest_GetAll_PrimitiveTypes()
- {
- string assemblyFullName = "System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
-
- Assembly assembly = Assembly.Load(assemblyFullName);
-
- IEnumerable<TypeInfo> primitiveTypes =
- assembly.DefinedTypes.Where(definedType => definedType.IsPrimitive && definedType != typeof(IntPtr) && definedType != typeof(UIntPtr));
-
- int totalPrimitiveTypes = primitiveTypes.Count();
-
- string[] dataPrimitiveTypes = new string[totalPrimitiveTypes];
-
- using (var provider = new CSharpCodeProvider())
- {
- dataPrimitiveTypes = primitiveTypes.Select(x => provider.GetTypeOutput(new CodeTypeReference(x))).ToArray();
- }
- this._output.WriteLine(StringOutputHelper.WriteString(dataPrimitiveTypes, '•'));
-
- Assert.True(totalPrimitiveTypes == 12);
- }
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.
-
- [Fact]
- public void UnitTest_GetAll_PrimitiveTypes_With_Information()
- {
- string assemblyFullName = "System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e";
-
- Assembly assembly = Assembly.Load(assemblyFullName);
-
- IEnumerable<TypeInfo> primitiveTypes =
- assembly.DefinedTypes.Where(definedType => definedType.IsPrimitive && definedType != typeof(IntPtr) && definedType != typeof(UIntPtr));
-
- var attr = BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.Static;
-
- var typeInformation = new List<TypeInformation> { };
-
- using (var provider = new CSharpCodeProvider())
- {
- foreach (var primitiveType in primitiveTypes)
- {
- switch (primitiveType.FullName)
- {
- case "System.SByte":
- case "System.Int16":
- case "System.Int32":
- case "System.Int64":
- case "System.Byte":
- case "System.UInt16":
- case "System.UInt32":
- case "System.UInt64":
- typeInformation.Add(new TypeInformation
- {
- Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
- DotNetType = primitiveType.FullName,
- MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),
- MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),
- TypeInformationCategory = TypeInformationCategory.IntegerPrimitiveTypes
- });
- break;
- case "System.Single":
- case "System.Double":
- case "System.Decimal":
- typeInformation.Add(new TypeInformation
- {
- Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
- DotNetType = primitiveType.FullName,
- MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),
- MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),
- TypeInformationCategory = TypeInformationCategory.FloatingPrimitiveTypes
- });
- break;
- case "System.Boolean":
- typeInformation.Add(new TypeInformation
- {
- Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
- DotNetType = primitiveType.FullName,
- MinValue = (primitiveType.GetField("FalseString", attr)).GetValue(primitiveType).ToString(),
- MaxValue = (primitiveType.GetField("TrueString", attr)).GetValue(primitiveType).ToString(),
- TypeInformationCategory = TypeInformationCategory.FloatingPrimitiveTypes
- });
- break;
- case "System.Char":
- typeInformation.Add(new TypeInformation
- {
- Alias_Name = provider.GetTypeOutput(new CodeTypeReference(primitiveType)),
- DotNetType = primitiveType.FullName,
- MinValue = (primitiveType.GetField("MinValue", attr)).GetValue(primitiveType).ToString(),
- MaxValue = (primitiveType.GetField("MaxValue", attr)).GetValue(primitiveType).ToString(),
- TypeInformationCategory = TypeInformationCategory.CharacterPrimitiveTypes
- });
- break;
- default:
- break;
- }
- }
- }
-
- public struct TypeInformation
- {
- public string Alias_Name { get; set; }
- public string DotNetType { get; set; }
- public string MinValue { get; set; }
- public string MaxValue { get; set; }
- public TypeInformationCategory TypeInformationCategory { get; set; }
-
- public override string ToString()
- {
- return string.Format("Name Or Alias: {0}{1}.NET Type: {2}{3}Min Value : {4}{5}Max Value: {6}{7}",
- this.Alias_Name,
- Environment.NewLine,
- this.DotNetType,
- Environment.NewLine,
- this.MinValue,
-
- Environment.NewLine,
- this.MaxValue,
- Environment.NewLine);
- }
-
- public static string GetOutput(List<TypeInformation> typeInformations)
- {
- StringBuilder output = new StringBuilder();
-
- foreach (var item in typeInformations)
- {
- output.AppendLine(item.ToString());
- }
-
- return output.ToString();
- }
- }
-
- [Flags]
- public enum TypeInformationCategory : byte
- {
- IntegerPrimitiveTypes,
- FloatingPrimitiveTypes,
- CharacterPrimitiveTypes,
- BooleanPrimitiveTypes
- }
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!