Reflection and Generic

R

Roger Odermatt

Hello

I have the follow Code.

---------------------------------

Dim myType As Type
myType = BusinessObject.GetType

Dim myP As System.Reflection.PropertyInfo
myP = myType.GetProperty("Codes")

Dim myObject As Object
myObject = myP.GetValue(BusinessObject, Nothing)

Dim myType2 As Type
myType2 = myObject.GetType

Dim myP2 As System.Reflection.PropertyInfo
myP2 = myType2.GetProperty("Addresses")

Dim myCol As Server.cCollectionBase(Of Server.cAddress)
myCol = myP2.GetValue(myObject, Nothing)
---------------------------------

Now can i make this line
[Dim myCol As Server.cCollectionBase(Of Server.cAddress)]

so that i can set the Of-Statement dynamic from the type of myP2 (not Of
Server.cAddress / separate Of myP2.GetType) ? When yes how make that?

regrads
Roger
 
H

Herfried K. Wagner [MVP]

Roger Odermatt said:
Dim myType As Type
myType = BusinessObject.GetType

Dim myP As System.Reflection.PropertyInfo
myP = myType.GetProperty("Codes")

Dim myObject As Object
myObject = myP.GetValue(BusinessObject, Nothing)

Dim myType2 As Type
myType2 = myObject.GetType

Dim myP2 As System.Reflection.PropertyInfo
myP2 = myType2.GetProperty("Addresses")

Dim myCol As Server.cCollectionBase(Of Server.cAddress)
myCol = myP2.GetValue(myObject, Nothing)
---------------------------------

Now can i make this line
[Dim myCol As Server.cCollectionBase(Of Server.cAddress)]

so that i can set the Of-Statement dynamic from the type of myP2 (not Of
Server.cAddress / separate Of myP2.GetType) ? When yes how make that?

Maybe 'Type.GetGenericTypeDefinition' solves your problem.
 
R

Roger Odermatt

Thank you very much, but how can us it with my declaration?

Dim myCol as Server.cCollectionBase( ?????? )

When i use the 'Type.GetGenericTypeDefinition' then i become the right type,
but only as string and i need the object.

regards
Roger


Herfried K. Wagner said:
Roger Odermatt said:
Dim myType As Type
myType = BusinessObject.GetType

Dim myP As System.Reflection.PropertyInfo
myP = myType.GetProperty("Codes")

Dim myObject As Object
myObject = myP.GetValue(BusinessObject, Nothing)

Dim myType2 As Type
myType2 = myObject.GetType

Dim myP2 As System.Reflection.PropertyInfo
myP2 = myType2.GetProperty("Addresses")

Dim myCol As Server.cCollectionBase(Of Server.cAddress)
myCol = myP2.GetValue(myObject, Nothing)
---------------------------------

Now can i make this line
[Dim myCol As Server.cCollectionBase(Of Server.cAddress)]

so that i can set the Of-Statement dynamic from the type of myP2 (not Of
Server.cAddress / separate Of myP2.GetType) ? When yes how make that?

Maybe 'Type.GetGenericTypeDefinition' solves your problem.
 
H

Herfried K. Wagner [MVP]

Roger Odermatt said:
Thank you very much, but how can us it with my declaration?

Dim myCol as Server.cCollectionBase( ?????? )

When i use the 'Type.GetGenericTypeDefinition' then i become the right
type, but only as string and i need the object.

Sorry, I misread your question.

You cannot dynamically type a variable at runtime. However, you can
dynamically instantiate a type at runtime using the function below:

\\\
Public Module Helpers
Public Function CreateGenericTypeInstance( _
ByVal GenericType As Type, _
ByVal ParameterTypes() As Type, _
Optional ByVal ThrowOnError As Boolean = False _
) As Object
Dim MaxIndex As Integer = ParameterTypes.Length - 1
Dim TypeName As String = GenericType.FullName
TypeName &= "["
For i As Integer = 0 To MaxIndex
TypeName &= "[" & ParameterTypes(i).AssemblyQualifiedName & "]"
If i < MaxIndex Then
TypeName &= ","
End If
Next i
TypeName &= "]"
Return _
Activator.CreateInstance( _
Type.GetType(TypeName, ThrowOnError) _
)
End Function
End Module
///

Usage:

\\\
Dim o As Object
o = _
CreateGenericTypeInstance( _
GetType(Foo(Of )), _
New Type() {GetType(Integer)}, _
True _
)
MsgBox(o.GetType().AssemblyQualifiedName)
o = _
CreateGenericTypeInstance( _
GetType(Dictionary(Of ,)), _
New Type() {GetType(String), GetType(Form)}, _
True _
)
MsgBox(o.GetType().AssemblyQualifiedName)
....
Public Class Foo(Of T)
'
End Class
///

Note that the target variable's type must be 'Object' or any base type of
the generic type being instantiated.
 
J

Jay B. Harlow [MVP - Outlook]

Roger,
| Now can i make this line
| [Dim myCol As Server.cCollectionBase(Of Server.cAddress)]
|
| so that i can set the Of-Statement dynamic from the type of myP2 (not Of
| Server.cAddress / separate Of myP2.GetType) ? When yes how make that?
Are you saying you want to do the equivalent of:

Dim aType As Type = GetType(sometype)
Dim myCol As Server.cCollectionBase(Of aType)

You should be able to use Type.MakeGenericType & Activator.CreateInstance,
something like:

Dim aType As Type = GetType(String) ' your dynamic sometype...

Dim theCollectionType As Type =
GetType(System.Collections.ObjectModel.Collection(Of ))

Dim myColType As Type = theCollectionType.MakeGenericType(aType)

Dim mycol As Object = Activator.CreateInstance(myColType)


HOWEVER: Without knowing the actual type at compile time, you will only be
able to use late binding & object variables with the created type, plus
possibly a handful of interfaces.


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Hello
|
| I have the follow Code.
|
| ---------------------------------
|
| Dim myType As Type
| myType = BusinessObject.GetType
|
| Dim myP As System.Reflection.PropertyInfo
| myP = myType.GetProperty("Codes")
|
| Dim myObject As Object
| myObject = myP.GetValue(BusinessObject, Nothing)
|
| Dim myType2 As Type
| myType2 = myObject.GetType
|
| Dim myP2 As System.Reflection.PropertyInfo
| myP2 = myType2.GetProperty("Addresses")
|
| Dim myCol As Server.cCollectionBase(Of Server.cAddress)
| myCol = myP2.GetValue(myObject, Nothing)
| ---------------------------------
|
| Now can i make this line
| [Dim myCol As Server.cCollectionBase(Of Server.cAddress)]
|
| so that i can set the Of-Statement dynamic from the type of myP2 (not Of
| Server.cAddress / separate Of myP2.GetType) ? When yes how make that?
|
| regrads
| Roger
|
|
 
H

Herfried K. Wagner [MVP]

Addendum:

.... and a much shorter solution:

\\\
Dim GenericType As Type = GetType(List(Of ))
Dim ParameterTypes() As Type = {GetType(Integer)}
Dim o As Object = _
Activator.CreateInstance( _
GenericType.MakeGenericType(ParameterTypes) _
)
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top