cast to generic type at runtime

2019-08-09 04:23发布

问题:

Ok, i don't think this is possible so I thought I would ask to make sure. I'm in the process of creating a function which reflects over the properties in a class and adds them to this data structure I have. Some of the properties are generic types.

So say we have DataType(Of T) that has a .Value property of type T:

Dim properties = GetType(MyType).GetFields(Reflection.BindingFlags.Public Or _
                                           Reflection.BindingFlags.Instance)
For Each prop As fieldinfo In properties
   Collection.Add(prop.Name,prop.GetValue(poco))
Next

In the collection.Add for primitive types (Integer, String, etc.…) I just want to add the type... but in the case of generic I want to add the DataType(Of T).Value. I hope there is some work-around but I don't think there is a way because the type of T can not be determined at compile time right? Ideally DirectCast(prop.getvalue(poco), DataType(Of T)).Value would be possible. This is when you hoped even more dynamics appear than whats in .NET 4.0.

回答1:

It just occurred to me that I may have mis-read your question. Therefore another answer from me.

You can avoid the need to cast to a generic type (where you don't know the type parameter) at runtime by introducing a new interface that allows access to some Value property:

Public Interface IHasValue
    Public ReadOnly Property Value As Object
End Interface

Next, make sure your DataType(Of T) class implements this interface. This means that each DataType(Of T) object (no matter what concrete type T is) is also an IHasValue, and you will be able to check this with TypeOf(…) Is IHasValue:

Public Class DataType(Of T) : Implements IHasValue

    Public Value As T

    Public ReadOnly Property UntypedValue As Object Implements IHasValue.Value
        Get
            Return Me.Value
        End Get
    End Property

End Class

(This duplication of Value is necessary for two reasons: First it is necessary when DataType(Of T).Value is a field instead of a property, and second because they do not have the same type.)

Then, where you fill your collection with fields, you can check whether a particular field is an DataType(Of T) (and if so, unwrap the Value from it) by doing the following:

Dim value As Object = prop.GetValue(poco)

If TypeOf(value) Is IHasValue Then         ' <- is value a DataType(Of T) ?    '
    value = CType(value, IHasValue).Value  ' <- if so, unwrap the value        '
End If

Collection.Add(prop.Name, value)

This is probably more what you're after than my other answer, but let me emphasise this:

If TypeOf(…) Is … Then … is a code smell. Perhaps you should re-think your code and investigate solutions involving polymorphism.



回答2:

First, my assumptions about what you're attempting to do:

  1. You have some object named poco, having type MyType.

  2. MyType has fields of different types, thus you think you need generics.

  3. You have a key-value-pair collection named Collection, probably having type Dictionary(Of String, Object)?

  4. You want to transfer all of poco's fields (ie., their names and values) to Collection.


Side node: You are mixing up fields with properties: Your code operates on fields (by invoking GetFields), while your question text speaks of properties. They are not the same thing!


Second, some fundamental facts about collections and the static type system:

  • Untyped collections (where items are of the type Object) can store all sorts of values in it.

  • Typed collections (where items are of a more specific type, e.g. T) can only store values having type T or a type derived from T.


Third, conclusions drawn from the above:

  • In your case, Collection must be an untyped collection, if it is indeed true that poco (having type MyType) has fields of varying types.

  • Generics won't actually help you at all in this case, because that's not what generics are for. Generics are useful when you want to define operations (ie. methods, behaviour) that work in the same way no matter what type of object they're working on.

    For example, List(Of T) defines a collection type that works the same no matter what type T is. But that does not mean that you can put anything in a List(Of T), because as soon as you instantiate this type — e.g. with Dim xs As New List(Of String) —, T will be fixed to a specific type — String —, and you end up with a typed collection that only accepts this type of values.

    So, again: If you need a collection that stores different kinds of objects, choose Object as the value type instead of trying to find a solution involving generics.


That being said, there is another solution: polymorphism.

If you want your code to handle values differently, depending on their type, polymorphism is the way to go:

  • First, you define an interface or an abstract base class (e.g. DataType, though I'd strongly suggest you choose a more self-explanatory name!) that specifies what can be done with your values.

  • Second, type Collection as Dictionary(Of String, DataType). This means that only objects of type DataType or anything derived from it can go into the collection.

  • Third, derive from/implement DataType to specify behaviour for a particular type.

There is a nice slide show called Conditionals and Polymorphism that may be of interest here.


Side note: Last but not least, you can find out the type of a generic type parameter: TypeOf(T).