开发者

How to determine is an object is a structure? No "GetType(Object).IsStructure function

开发者 https://www.devze.com 2023-02-15 00:43 出处:网络
I\'m looking for a similar \"IsStructure\" function. Is there some other way to determine if T is a structure but not an intrinsic type?

I'm looking for a similar "IsStructure" function. Is there some other way to determine if T is a structure but not an intrinsic type?

Public Shared Function MySub(Of TData)(ByVal t As TData) As TData
    Dim IsClass As Boolean
    IsClass =开发者_C百科 GetType(TData).IsClass
End Function

Note that using IsPrimitive and IsValueType on Nullable(Of Integer) and a Structure returns the same results, False and True, respectively.


Type.IsValueType and Type.IsPrimitive should do the trick for you.


.IsClass

http://msdn.microsoft.com/en-us/library/system.type.isclass.aspx

.IsValueType

http://msdn.microsoft.com/en-us/library/system.type.isvaluetype.aspx

Edit -

Short Linqpad program:

void Main()
{
    var a = new A();
    var b = new B();

    a.GetType().IsValueType.Dump("class IsValueType");
    a.GetType().IsClass.Dump("class IsClasse");
    a.GetType().IsPrimitive.Dump("class IsPrimitive");

    b.GetType().IsValueType.Dump("struct IsValueType");
    b.GetType().IsClass.Dump("struct IsClasse");
    b.GetType().IsPrimitive.Dump("struct IsPrimitive");
}

class A{}
struct B{}

Results

class IsValueType

False


class IsClasse

True


class IsPrimitive

False


struct IsValueType

True


struct IsClasse

False


struct IsPrimitive

False
0

精彩评论

暂无评论...
验证码 换一张
取 消