| « Custom Paging with DataTable | Silverlight 3 Tutorial on Moveable background/terrain » |
Runtime Method/Class Call
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
This post has 8 feedbacks awaiting moderation...