Red Taz
Red Taz

Reputation: 4179

How do you get the Type of any basic system object or primitive

I'm trying to intelligently populate an object from the results of a SQL query. I'm looping through the results of a SQLDataReader and using GetDataTypeName() to get the string representation of the data type held in any particular field.

How can I automatically get the Type of each field without manually mapping string names to types. I've tried using:

Type.GetType(reader.GetDataTypeName(), true, true)

(Note, case-sensitivity is ignored)

But GetDataTypeName() returns strings of primitives such as like "int" and these throw TypeLoadExceptions for the example given.

I've read that GetType() requires an assembly qualified type name so non-primitive system types like DateTime would need to be qualified with "System.DateTime", however simply prefixing the "System" namespace with "int" doesn't help for a primitive type such as this.

Upvotes: 0

Views: 55

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063318

There is no need to parse anything here; just use:

Type type = reader.GetFieldType(columnIndex);

Upvotes: 3

Related Questions