C# 如何以编程方式检查类型是结构还是类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1827425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check programmatically if a type is a struct or a class?
提问by Jader Dias
How to check programmatically if a type is a struct or a class?
如何以编程方式检查类型是结构还是类?
采纳答案by Andrew Hare
Use Type.IsValueType
:
Gets a value indicating whether the Type is a value type.
获取一个值,该值指示 Type 是否为值类型。
Use it either like this:
像这样使用它:
typeof(Foo).IsValueType
or at execution time like this:
或在执行时像这样:
fooInstance.GetType().IsValueType
Conversely there is also a Type.IsClass
property (which should have been called IsReferenceType
in my opinion but no matter) which may or may not be more appropriate for your uses based on what you are testing for.
相反,还有一个Type.IsClass
属性(IsReferenceType
在我看来应该被称为但无论如何),根据您的测试目的,它可能更适合您的用途,也可能不适合您。
Code always seems to read better without boolean negations, so use whichever helps the readability of your code.
代码在没有布尔否定的情况下似乎总是更好读,所以使用任何有助于代码可读性的方法。
As Stefan points out below, in order to properly identify structsyou must be careful to avoid false positives when it comes to enums
. An enum
is a value type so the IsValueType
property will return true
for enums
as well as structs
.
正如 Stefan 在下面指出的那样,为了正确识别结构,您必须小心避免在涉及enums
. 一个enum
是值类型,这样IsValueType
属性将返回true
的enums
还有structs
。
So if you truly are looking for structs
and not just value types in general you will need to do this:
因此,如果您真的正在寻找structs
而不仅仅是一般的值类型,您将需要这样做:
Type fooType = fooInstance.GetType();
Boolean isStruct = fooType.IsValueType && !fooType.IsEnum;
回答by JaredPar
Try the following
尝试以下
bool IsStruct(Type t) {
return t.IsValueType;
}
回答by Stefan Steinegger
Type type = typeof(Foo);
bool isStruct = type.IsValueType && !type.IsPrimitive;
bool isClass = type.IsClass;
It could still be: a primitive type or an interface.
它仍然可以是:原始类型或接口。
Edit:There is a lot of discussion about the definition of a struct. A struct and a value type are actually the same, so IsValueType
is the correct answer. I usually had to know whether a type is a user defined struct, this means a type which is implemented using the keyword struct
and not a primitive type. So I keep my answer for everyone who has the same problem then me.
编辑:有很多关于结构定义的讨论。结构体和值类型实际上是相同的,IsValueType
正确答案也是如此。我通常必须知道类型是否是用户定义的 struct,这意味着使用关键字struct
而不是原始类型实现的类型。所以我为所有和我有同样问题的人保留我的答案。
Edit 2: According to the C# Reference, enums are not structs, while any other value type is. Therefore, the correct answer how to determine if a type is a struct is:
编辑 2:根据C# 参考,枚举不是结构,而任何其他值类型是。因此,如何确定类型是否为结构体的正确答案是:
bool isStruct = type.IsValueType && !type.IsEnum;
IMHO, the definition of a struct is more confusing then logical. I actually doubt that this definition is of any relevance in praxis.
恕我直言,结构的定义比逻辑更混乱。我实际上怀疑这个定义在实践中是否有任何相关性。
回答by supercat
For every value type, there is a corresponding auto-generated class type which derives from System.ValueType
, which in turn derives from System.Object
. Note that value types themselves do not derive from anything, but are implicitly convertibleto that class type, and instances of that class type may be explicitly converted to the value type.
对于每个值类型,都有一个相应的自动生成的类类型,它派生自System.ValueType
,而后者又派生自System.Object
。请注意,值类型本身不派生自任何东西,但可以隐式转换为该类类型,并且该类类型的实例可以显式转换为值类型。
Consider:
考虑:
public static int GetSomething<T>(T enumerator) where T : IEnumerator<int> { T enumerator2 = enumerator; enumerator.MoveNext(); enumerator2.MoveNext(); return enumerator2.Current; }
Calling this routine on a variable of type List<int>.Enumerator
will yield very different behavior from calling it on a variable of type IEnumerator<int>
which happens to have an instance of List<int>.Enumerator
stored within it. Even though a variable of type List<int>.Enumerator
is a value type, an instance of List<int>.Enumerator
stored in a variable of type IEnumerator<int>
will behave as a class type.
在一个类型的变量上调用这个例程List<int>.Enumerator
将产生与在一个类型的变量上调用它的非常不同的行为,该类型的变量IEnumerator<int>
恰好在其中List<int>.Enumerator
存储了一个 的实例。即使类型变量List<int>.Enumerator
是值类型,List<int>.Enumerator
存储在类型变量中的实例IEnumerator<int>
也将表现为类类型。
回答by toddmo
Extension method. It returns true
for anything defined as a struct
in my code but not for things like int
which although they are technically structs are not for my purposes.
扩展方法。它返回true
任何struct
在我的代码中定义为 a的东西int
,但不会返回那些虽然它们在技术上是结构体并不适合我的目的的东西。
I needed to know when a type may have child fields or properties but was defined as a struct
and not a class
. Because when you alter a struct
it just alters a copy, and then you have to set the original back to the altered copy to make the changes "stick".
我需要知道什么时候一个类型可能有子字段或属性但被定义为 astruct
而不是 a class
。因为当您更改 a 时,struct
它只会更改一个副本,然后您必须将原件设置回更改后的副本以使更改“保持不变”。
public static bool IsStruct(this Type source)
{
return source.IsValueType && !source.IsPrimitive && !source.IsEnum;
}