5

How do i check if a Type is a struct? IsClass worked perfectly then my reflection stop behaving as i expected when i change the class to the struct. How do i check if its a struct? -edit- i need to check for class/structs. Nothing else, i cant match longs, ints, etc by accident. DateTime may be ok i am using this to search a Attribute/FieldType inside of the type i am checking

1 Answers1

11

Type.IsValueType should do the trick.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • Now long and ints are confused with structs. –  Jan 17 '10 at 23:06
  • 11
    @acidzombie24: `long` and `int` **are** structs. What confusion? Maybe you mean `IsPrimitive`? – Marc Gravell Jan 17 '10 at 23:07
  • I can write IsPrimitive == false (as a shortcut and i have an alternative to most primitives) and use IsValueType. Thanks :) –  Jan 17 '10 at 23:11
  • 3
    acidzombie24: note this will depend on what you want to exclude. For example, `decimal` is **NOT** a primitive, even though it looks that way in C#. Hopefully this won't be an issue for your use case... – itowlson Jan 17 '10 at 23:13
  • itowlson: Test are all in place and decimal isnt an issue. There are select types i support and user structs are one but only if it has certain attributes :) –  Jan 17 '10 at 23:25