Posted by: Sourav | September 3, 2016

Late Binding using Reflection in VB,VB Teacher Sourav,Kolkata 09748184075


Option Strict On
Option Explicit On
Imports System
Imports System.Reflection

Module Module1

Sub Main()
‘To load the assembli at runtime we need to use the assembly class,
‘getexecutingassembly looks to find the current executing assembly
Dim executingassembly As Assembly = Assembly.GetExecutingAssembly()
‘Once you have the executive assembly within that assembly we need to find the customer class
‘to be instantiated dynamically at run time
‘to do this,here gettype is turning a Type and to catch it
Dim customertype As Type = executingassembly.GetType(“latebindvb.customer”)
‘Once we found the type we need to instantiate the type,if the methods is the type are shared
‘then we need not instantiate the type,but the methods are instance methods so we
‘need to instantiate the type,but when we are instantiating a type we don’t know which type it is,so
‘to catch the type we need to catch it in a type which is the parent of all
‘and that is object type
Dim customerinstance As Object = Activator.CreateInstance(customertype)

‘The next thing to do is to get the method/function information ,that is the getfullname function in
‘the customer class
‘this method expects two parameters

Dim getfullnamefucntion As MethodInfo = customertype.GetMethod(“getfullname”)
Dim parameters() As String = {“Sourav”, “Bhattacharya”}
Dim objparam(parameters.Length – 1) As Object
parameters.CopyTo(objparam, 0)
‘ objparameters = New Object() {parameters(0), parameters(1)}

‘Console.WriteLine(objparameters(0))

‘Ok last step is to invoke the method,but using the customerinstance object we can not do that,because the class might not exist at compile time

‘by intellisense you will see this invoke method expects object array and not string array
‘which is justified because it is in more generic form as the object is the parent of all,but string class is
‘ a child class of object class,so in place of an object array we can pass a string array

‘However the invoke returns an object which can not be captured by a string class object,because
‘object is parent and string is child,so we need to cast it explicitly by cstr()

‘    Dim fullname As String = CStr(getfullnamefucntion.Invoke(customerinstance, New Object() {“Sourav”, “Bhattacharya”}))

Dim fullname As String = CStr(getfullnamefucntion.Invoke(customerinstance, objparam))

Console.WriteLine(“The fullname is {0}”, fullname)
Console.ReadLine()
‘reference
http://stackoverflow.com/questions/33901047/how-can-i-pass-an-integer-array-to-a-method-reflection-c-sharp
https://bytes.com/topic/net/answers/415161-there-way-convert-array-objects-array-strings

End Sub

End Module

Public Class customer
Public Function getfullname(ByVal firstname As String, ByVal lastname As String) As String
Return firstname + ” ” + lastname

End Function
End Class

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: