Reflection

H

harry

Hi,

Has anyone a link or basic intro to Reflection?

I need to find and instantiate a particular class based on user input at run
time.

For instance, Class1 may add 2 numbers, Class2 may subtract 2 numbers. Only
the user at runtime selects which they wish to use. How would I find the
correct class?

This is just a simple example of my requirement. In the simple scenario
above I'd just hard code it, but what if I had 1,000 classes with different
algorythms, how would I find the correct one? For instance, can I select a
class by name at runtime?

I hope this explanation makes sense.

Thanks
Harry
 
H

Herfried K. Wagner [MVP]

* "harry said:
I need to find and instantiate a particular class based on user input at run
time.

For instance, Class1 may add 2 numbers, Class2 may subtract 2 numbers. Only
the user at runtime selects which they wish to use. How would I find the
correct class?

I am not sure what you are referring to, but maybe this sample helps:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 
H

harry

Thanks Herfried. It's close to my requirement, however what if the classes
are made from scratch and not based on any other class?

\\\
Public Class MyClass

Public Sub(ByVal Test as string)
'...
End Sub

Public Function MyFunction()
'...
End Function

End Class
\\\

In this situation, how can I set the QualifiedClassName. Is there a way I
can use a string and have it converted like the PartialAssemblyName. I guess
I would want something like:

Dim c As Control = _
DirectCast( _
CreateClassByName( _
"MyClass", _
"MyClass" _
), _
Control _
)

Or should it be Dim O as Object = ?

Is there any way I could achive this?

Thanks
Harry



Herfried K. Wagner said:
* "harry said:
I need to find and instantiate a particular class based on user input at run
time.

For instance, Class1 may add 2 numbers, Class2 may subtract 2 numbers. Only
the user at runtime selects which they wish to use. How would I find the
correct class?

I am not sure what you are referring to, but maybe this sample helps:

\\\
Private Function CreateClassByName( _
ByVal PartialAssemblyName As String, _
ByVal QualifiedClassName As String _
) As Object
Return _
Activator.CreateInstance( _
[Assembly].LoadWithPartialName( _
PartialAssemblyName _
).GetType(QualifiedClassName) _
)
End Function
///

Usage:

\\\
Dim c As Control = _
DirectCast( _
CreateClassByName( _
"System.Windows.Forms", _
"System.Windows.Forms.Button" _
), _
Control _
)
With c
.Location = New Point(10, 10)
.Size = New Size(80, 26)
.Text = "Hello World"
End With
Me.Controls.Add(c)
///
 

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