« Custom Paging with DataTableSilverlight 3 Tutorial on Moveable background/terrain »

Runtime Method/Class Call

Permalink 03/10/10 12:04, by admin, Categories: News, Visual Basic, .Net , Tags: assembly.getassembly, constructorinfo.invoke, reflection, runtime class instantiation, runtime method calls, type.invokemember, vb.net

Link: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/2c69b058-f062-4491-acd2-1c497a38329f

Recently, i found myself wanting to dynamically (runtime) construct a method from a class and then call that method without developing a nasty logic structure.

In essence, i wanted to perform this logic, based on known method/class naming:

Dim tmpMethod As String = "Get"
Dim tmpParameters() As Object
tmpMethod &= core.Type.ToString
If Not (core.Shift = Nothing) Then
tmpMethod &= "ByShift"
tmpParameters = New Object() {CType(core.Line, Byte), CType(core.Shift, Byte)}
Else
tmpParameters = New Object() {CType(core.Line, Byte)}
End If

without all the overhead of performing nested If..Then statements, or worse a structure Select...Case logic.

What i came up with is the use of a very high-level programming reflection. With the help of everyone at the MSDN Forums, they were able to push and help me to get this developed.

Below is what ultimately was developed. Although the InvokeMethod method is pretty much nailed down to a Component type (DataAdapter, TableAdapter, etc), it can be massaged for any type and made into a Generic to fit more uses. Although the InvokeMember method can not operate on Generic Methods themselves, you should be able to wrap it with your own generics to conform to your structure

Copied the code below from another site, go here

Public Shared Function CreateInstance(ByVal [type] As Type, ByVal [class] As String, ByVal args() As Object) As Object
Dim ass As Reflection.Assembly = Reflection.Assembly.GetAssembly([type])
Try
For Each t As Type In ass.GetTypes
If t.IsClass = True Then
If t.FullName.EndsWith("." + [class]) Then
Dim con() As Reflection.ConstructorInfo = t.GetConstructors
For Each constr As Reflection.ConstructorInfo In con
If constr.GetParameters.Length = args.Length Then
Return constr.Invoke(args)
End If
Next
End If
End If
Next
Return Nothing
Catch ex As Exception
Throw New System.Exception("could not create instance ---->" + [class])
Return Nothing
End Try
End Function

Copied most of this logic from the MSDN site and pieced together from the MSDN Support inquiry that i submitted.

Public Shared Function InvokeMethod(Of T)(ByVal typeName As ComponentModel.Component, ByVal methodName As String, ByVal param() As Object) As T
Dim [class] As String = typeName.ToString.Substring(typeName.ToString.LastIndexOf(".") + 1)
'create an instance of the object of typeName by its class alone
Dim instance As Object = CreateInstance(typeName.GetType, [class], New Object() {})
'get the type of the object
Dim invoker As Type = instance.GetType

Dim result As Object
result = invoker.InvokeMember(methodName, Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.InvokeMethod, Nothing, instance, param)

Return CType(result, T)
End Function

1 comment »

1 comment

Comment from: Term Papers [Visitor]
good to see this blog ...
04/29/10 @ 03:13

This post has 8 feedbacks awaiting moderation...

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)

AdSense

Programming Blog about the different projects we are working on.

Search

February 2012
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

XML Feeds

powered by b2evolution free blog software