Class or structure

How can I check type or type structure?

 protected   T GetNullValue<T>(IDataReader reader, int ordinalId)
    {
        if (reader.IsDBNull(ordinalId))
        {
            //if T is struct.....
            //else if I is class
        }
        return (T)reader.GetValue(ordinalId);
    }
+3
source share
3 answers
if (default(T) is ValueType)
   ...

is the most effective thing I can think of at the moment.

+7
source

Get the Typeclass for the object and check it.

Type t = reader.GetValue(ordinalId).GetType();
if (t.IsValueType){
    //Struct
} else { 
    //Class
}

I suspect you'll be using the Type object later in your code if you are trying to dynamically process the results.

+3
source

If T is a value type, it cannot be null. To make the value type nullable, you must use a System.Nullable struct.

+2
source

All Articles