I am writing a simple List<t>
to CSV converter. My converter checks the all the t
's in List and grabs all public properties and places them into the CSV.
My code works great (as intended) when you will use a simple class with a few properties.
I would like to get the List<t>
to CSV converter to also accept the System types such as String and Integer. With these system types I do not want to get their public properties (such as Length, Chars etc). Thus I would like to check if the object is a System type. By System type I mean one of the built in .Net types such as string, int32, double
etc.
Using GetType() I can find out the following:
string myName = "Joe Doe"; bool isPrimitive = myName.GetType().IsPrimitive; // False bool isSealed = myName.GetType().IsSealed; // True // From memory all of the System types are sealed. bool isValueType = myName.GetType().IsValueType; // False // LinqPad users: isPrimitive.Dump();isSealed.Dump();isValueType.Dump();
How can I find if variable myName is a built in System type? (assuming we don't know its a string)
Examples. The following example creates an instance of a type and indicates whether the type is a class. type MyDemoClass = class end try let myType = typeof<MyDemoClass> // Get and display the 'IsClass' property of the 'MyDemoClass' instance. printfn $"\nIs the specified type a class? {myType.
C# provides a standard set of built-in types. These represent integers, floating point values, Boolean expressions, text characters, decimal values, and other types of data. There are also built-in string and object types. These types are available for you to use in any C# program.
Here are a few of the several possibilities:
myName.GetType().Namespace == "System"
myName.GetType().Namespace.StartsWith("System")
myName.GetType().Module.ScopeName == "CommonLanguageRuntimeLibrary"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With