For a Type
, there is a property IsClass
in C#, but how to decide a Type
is a struct?
Although IsValueType
is a necessary condition, it is obviously not enough. For an int
is a value type also.
Someone suggests the following code:
bool IsStruct = type.IsValueType && !type.IsEnum && !type.IsPrimitive;
But I am not sure whether it is an accurate method. The formula should tell the difference between struct and other types such as DateTime
, int
and array
s.
As some friends have pointed out that here, I mean user defined struct and not predefined types, such as DateTime
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…