How to determine if Type is a struct?

前端 未结 3 679
-上瘾入骨i
-上瘾入骨i 2021-01-17 19:22

Given a PropertyInfo instance, which has a Type property, how does one determine if it is a struct? I found there are properties such as IsPr

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-17 20:00

    putting the comments on Antony Koch 's answer into an extention method:

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

    should work

提交回复
热议问题