How to determine if a type is a structure?

Given an instance of PropertyInfo that has a Type property, how do I determine if this is a structure? I found that there are properties like IsPrimitive , IsInterface , etc., but I'm not sure how to query the structure?

EDIT: Clarify the question. Suppose I have a method:

 public Boolean Check(PropertyInfo pi) { return pi.Type.IsStruct; } 

What do I write instead of IsStruct ?

+7
c # types struct
source share
3 answers

Type.IsValueType should do the trick.

(sandwiched from here )

+10
source share

Adding comments to Anthony Koch in the extension method:

 public static class ReflectionExtensions { public static bool IsCustomValueType(this Type type) { return type.IsValueType && !type.IsPrimitive && type.Namespace != null && !type.Namespace.StartsWith("System."); } } 

must work

+1
source share

Structures and enumerations ( IsEnum ) fall under a superset called value types ( IsValueType ). Primitive types ( IsPrimitive ) are a subset of the structure. This means that all primitive types are structures, but not vice versa; for example, int is a primitive type as well as a structure, but decimal is only a structure, not a primitive type.

So you see that the only missing property is structure. Easy to write:

 public bool IsStruct(this Type type) { return type.IsValueType && !type.IsEnum; } 
+1
source share

All Articles