Returns a generic type of unspecified type

Is it possible to create a function that returns the generic type of an unspecified type?

Public Function Create(ByVal type As Type) As DeserializationHelper(Of )
    Dim DynamicHelper = GetType(DeserializationHelper(Of )).MakeGenericType(type)

    Return Activator.CreateInstance(DynamicHelper)
End Function

Everything works here, except for strict input of the return value. I could return it as an object, not a common interface or a base class that is not shared, but I am trying to create a very convenient succint API.

I am trying to create the syntax as follows:

Dim Loader As New XLoader(parentContainer, Map)
Dim MyCreature As Creature = Loader.Create(GetType(Creature)).From(xml.<Creature>)

... And yes, I know that there are built-in XML serializers - I would like them to work for me, but I need more control .;)

If I moved this part to another project and wrote it in C # (and then used it from the VB.NET project), would there be a C # Dynamic keyword at all?

Thank.:)

+3
source share
1 answer

Not an expert with VB, but hopefully this helps:

Public Function Create(Of TResult)() As DeserializationHelper(Of TResult)
    Return Activator.CreateInstance(Of DeserializationHelper(Of TResult))()
End Function
+6
source

All Articles