简体   繁体   English

如何确定对象是否为值类型

[英]How to determine whether the object is a value type

I could use Type.IsValueType to figure this out in a straightforward way in .net 4.5, but when I create universal apps using the portable library, it doesn't have any equivalent method to find if the type is value or not. 我可以使用Type.IsValueType在.net 4.5中以直接的方式解决这个问题,但是当我使用可移植库创建通用应用程序时,它没有任何等效方法来查找类型是否为值。

Is there any other trick to finding this? 找到这个还有其他技巧吗?

Try type.GetTypeInfo().IsValueType . 尝试type.GetTypeInfo().IsValueType Also make sure you have a using statement for System.Reflection, so the GetTypeInfo() extension method is available. 还要确保您具有System.Reflection的using语句,因此GetTypeInfo()扩展方法可用。

One workaround I found is: 我找到的一个解决方法是:

typeof(ValueType).IsAssignableFrom(type) typeof运算(值类型).IsAssignableFrom(类型)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM