开发者

How do I determine if a value is an instance of a generic type, ignoring the type parameter, in vb.net?

I have a class C(Of T). I want to determine if some given value has type C, regardless of what T is. For example, I might want to determine if a value is a strongly-typed 开发者_JAVA百科list, regardless what type of items the list stores.

I just need to know how to do it in VB.net. In Java the syntax is like this:

var result = obj instanceof Gen2<?>;


I believe a compact solution for your problem would be:

Dim result = (obj.GetType().GetGenericTypeDefinition().Equals(GetType(Gen2(Of ))))

Explanation:

  1. Gets the Type object representing the base type of instance obj
  2. Gets the generic type underlying the compiler instance type.
  3. Gets the generic type of Gen2 without a qualifying parameter.
  4. Compares the two generics to see if they are equal and returns the result.

It's not nearly as compact as the Java solution you posted (unless I'm mistaken, C# doesn't support either the instanceof keyword or the Java generic wildcard syntax), but it will work.

Edit: Prompted by Cory Larson's comment below, I should add that while the method I posted only works for directly comparing the generic to a known generic type, if you want to find out if it implements a generic interface, use:

Dim result = (obj.GetType().GetGenericTypeDefinition().GetInterface(GetType(IMyGeneric(Of )).FullName) IsNot Nothing)


Sure, sort of. For example:

    Dim obj As IList(Of Double) = New List(Of Double)
    Dim result As Boolean = obj.GetType().IsGenericType AndAlso _
        obj.GetType().GetGenericTypeDefinition().Equals(GetType(IList(Of )))

For that, the result is False. If you change the comparison from IList(Of ) to just List(Of ), then it works.

    Dim obj As IList(Of Double) = New List(Of Double)
    Dim result As Boolean = obj.GetType().IsGenericType AndAlso _
        obj.GetType().GetGenericTypeDefinition().Equals(GetType(List(Of )))

Will return True.

EDIT: Dang, Dan Story got it first.


If you are seeking to find out whether a type is a Foo(Of T) because you're interested in using some property which does not depend upon T, I would suggest that you should make that property available in either a non-generic base class or a non-generic interface. For example, if defining an ISuperCollection(Of T) which provides array-like access, one could offer a non-generic ISuperCollection collection which implements methods Count, RemoveAt, CompareAt, SwapAt, and RotateAt (calling RotateAt(4,3,1) would rotate three items, starting at item 4, up one spot, thus replacing item 5 with 4, 6 with 5, and 4 with the old value of 6), and have ISuperCollection(Of T) inherit from that.

BTW, if you segregate reader interfaces from writer interfaces, the reader interfaces can be covariant and the writer interfaces contravariant. If any property or indexer implements both read- and write- functions, you'll need to define a read-write interface which includes read-write implementations of any such property or indexer; a slight nuisance, but IMHO worth the small additional effort.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜