Determine that a System.Type is a Structure or not

  • Thread starter Thread starter sos00
  • Start date Start date
S

sos00

Hi guys,
How can i detect that a System.Type is a structure or not !?
i couldn't find any suitable function or property to get that (something
like IsClass property) !


Thanx
 
Thanks again, i tested it but primitive system types are valid in this
validation
for example System.String, int, bool and so on...

they are Value Type
they are Sealed
and thery are not Enum
 
sos00 said:
Thanks again, i tested it but primitive system types are valid in this
validation
for example System.String, int, bool and so on...

they are Value Type
they are Sealed
and thery are not Enum

System.String is *not* a value type. Int and bool etc are indeed sealed
non-enum value types - they're structures, so they *should* pass the
test.
 
The problem is that there aren't REALLY any "primitive" types, int is System.Int32,
bool is System.Boolean, string is System.String... They are all classes
or structs with constructors and methods, unlike an int in C++ where you're
really compiling down to a primitive type.

int and bool are structs.... from the msdn documentation

public struct Int32 : IComparable, IFormattable, IConvertible,
IComparable<int>, IEquatable<int>

public struct Boolean : IComparable, IConvertible, IComparable<bool>,
IEquatable<bool>


string is not a value type... from MSIL decompiler (valuetypes extend ValueType,
not object)

..class public auto ansi serializable sealed beforefieldinit String
extends object
implements System.IComparable, System.ICloneable, System.IConvertible,
System.Collections.IEnumerable
{
 
The problem is that there aren't REALLY any "primitive" types

There are - there are types that the CLI knows about directly - types
that there are specific IL instructions to handle. That's what the
Type.IsPrimitive property is all about.

Put it this way - if you don't count "int" as being a primitive type,
what would have to be different for you to count it as primitive?
 
I stand corrected...

in that case, if you want to find if a type is a Struct, but NOT one of the
"primitives",

t.IsValueType && !t.IsEnum && t.IsSealed && !t.IsPrimitive
 
Back
Top