Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a object type is a built in system type

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)

like image 495
Jeremy Avatar asked May 09 '11 04:05

Jeremy


People also ask

How do you check if property is of type is a class?

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.

What is type system in C#?

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.


1 Answers

Here are a few of the several possibilities:

  • myName.GetType().Namespace == "System"
  • myName.GetType().Namespace.StartsWith("System")
  • myName.GetType().Module.ScopeName == "CommonLanguageRuntimeLibrary"
like image 185
Gabe Avatar answered Oct 14 '22 15:10

Gabe