473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reflection - Run Function From String

I wish to execute code from a string. The string will have a function name,
which will return a string:

Dim a as string
a = "MyFunctionName(param1, param2)"

I have seen a ton of people discuss how reflection does this, but I cannot
find the syntax to do this. I have tried several code example off of
gotdotnet and other articles. Can somebody please show me the code to do
this?

Thank You,
Derek
Nov 12 '05 #1
9 1168
I have seen this described so that a function from another assembly can be
loaded, but I know the function will be in the compiled assembly that runs
the main program. I am still searching for somebody that has coded this . .
..

Any ideas?

Derek
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Derek,
I have seen a ton of people discuss how reflection does this, but I cannotfind the syntax to do this. I have tried several code example off of
gotdotnet and other articles. Can somebody please show me the code to do
this?


There's no really easy way to do this. You'll have to parse out the
function name and parameters, look up the method you want to call,
convert the parameter types if needed, and then call the function.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 12 '05 #2
SR
Hi

Is ur reqmt that "A Function Name will be passed at
runtime and you need to execute that function using
reflection?".Please confirm...If that is the case here u go

The fnExecuteMethods takes an object, a method name and
optionally parameters and invokes those methods

Public Sub CallMethodsThruReflection()

'No Parameters
fnExecuteMethods(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethods(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethods(Me, "fn3", l_objParams3)
'One String Parameter and One Integer Parameter
Dim l_objParams4(1) As Object
l_objParams4(0) = "Hi"
l_objParams4(1) = 20
fnExecuteMethods(Me, "fn4", l_objParams4)
'One String Parameter and One Integer Parameter
and One String as return Parameter
Dim l_objParams5(1) As Object
l_objParams5(0) = "Hi"
l_objParams5(1) = 20
Dim l_strRetVal As String = fnExecuteMethods
(Me, "fn5", l_objParams5).ToString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethods(ByVal p_objObject As
Object, ByVal p_objMethodName As String, Optional ByVal
p_objParams() As Object = Nothing) As Object

' Create a type object and store the type of the
object passed
Dim l_objType As Type = p_objObject.GetType
'Declare a MethodInfo object to store the Methods
of the class
Dim l_objMethodInfo As MethodInfo
'Declare a variable to loop through the Methods of
this class
Dim l_intMethodCtr As Integer

' Get the MethodInfo for the current class.
Binding Flags are specified to get the
' public and private Methods of this class. When
Public or Non-Public is specified
' in the BindingFlags, it is also necessary to
specify Static or Instance
l_objMethodInfo = l_objType.GetMethod
(p_objMethodName, BindingFlags.NonPublic Or
BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)

Return l_objMethodInfo.Invoke(p_objObject,
p_objParams)

End Function

Public Sub fn1()
print("fn1 was called")
End Sub

Public Sub fn2(ByVal p_strData As String)
print("fn2 was called with parameter : " &
p_strData)
End Sub

Public Sub fn3(ByVal p_intData As Integer)
print("fn3 was called with parameter : " &
p_intData)
End Sub

Public Sub fn4(ByVal p_strData As String, ByVal
p_intData As Integer)
print("fn4 was called with parameters : " &
p_strData & " AND " & p_intData)
End Sub

Public Function fn5(ByVal p_strData As String, ByVal
p_intData As Integer) As String
print("fn5 was called with parameters : " &
p_strData & " AND " & p_intData & ". Returning a
concatenated value.")
Return p_strData & p_intData
End Function

Public Sub print(ByVal p_objObject As Object)
Console.WriteLine(p_objObject)
End Sub

Hope this helps

regards,

sr
-----Original Message-----
I have seen this described so that a function from another assembly can beloaded, but I know the function will be in the compiled assembly that runsthe main program. I am still searching for somebody that has coded this . ...

Any ideas?

Derek
"Mattias Sjögren" <ma********************@mvps.org> wrote in messagenews:%2****************@TK2MSFTNGP11.phx.gbl...
Derek,
>I have seen a ton of people discuss how reflection does this, but I
cannot
>find the syntax to do this. I have tried several code
example off of >gotdotnet and other articles. Can somebody please show me the code to do >this?


There's no really easy way to do this. You'll have to

parse out the function name and parameters, look up the method you want to call, convert the parameter types if needed, and then call the function.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

.

Nov 12 '05 #3
Hi, thank you for responding. When I paste the code in there are a ton of
errors. Should I paste this into a global module? Can you email me this in
a module or a VB.Net app? Thank you so much for responding. Nobody has
been able to answer this.
"SR" <rs*****@hotmail.com> wrote in message
news:03****************************@phx.gbl...
Hi

Is ur reqmt that "A Function Name will be passed at
runtime and you need to execute that function using
reflection?".Please confirm...If that is the case here u go

The fnExecuteMethods takes an object, a method name and
optionally parameters and invokes those methods

Public Sub CallMethodsThruReflection()

'No Parameters
fnExecuteMethods(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethods(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethods(Me, "fn3", l_objParams3)
'One String Parameter and One Integer Parameter
Dim l_objParams4(1) As Object
l_objParams4(0) = "Hi"
l_objParams4(1) = 20
fnExecuteMethods(Me, "fn4", l_objParams4)
'One String Parameter and One Integer Parameter
and One String as return Parameter
Dim l_objParams5(1) As Object
l_objParams5(0) = "Hi"
l_objParams5(1) = 20
Dim l_strRetVal As String = fnExecuteMethods
(Me, "fn5", l_objParams5).ToString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethods(ByVal p_objObject As
Object, ByVal p_objMethodName As String, Optional ByVal
p_objParams() As Object = Nothing) As Object

' Create a type object and store the type of the
object passed
Dim l_objType As Type = p_objObject.GetType
'Declare a MethodInfo object to store the Methods
of the class
Dim l_objMethodInfo As MethodInfo
'Declare a variable to loop through the Methods of
this class
Dim l_intMethodCtr As Integer

' Get the MethodInfo for the current class.
Binding Flags are specified to get the
' public and private Methods of this class. When
Public or Non-Public is specified
' in the BindingFlags, it is also necessary to
specify Static or Instance
l_objMethodInfo = l_objType.GetMethod
(p_objMethodName, BindingFlags.NonPublic Or
BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)

Return l_objMethodInfo.Invoke(p_objObject,
p_objParams)

End Function

Public Sub fn1()
print("fn1 was called")
End Sub

Public Sub fn2(ByVal p_strData As String)
print("fn2 was called with parameter : " &
p_strData)
End Sub

Public Sub fn3(ByVal p_intData As Integer)
print("fn3 was called with parameter : " &
p_intData)
End Sub

Public Sub fn4(ByVal p_strData As String, ByVal
p_intData As Integer)
print("fn4 was called with parameters : " &
p_strData & " AND " & p_intData)
End Sub

Public Function fn5(ByVal p_strData As String, ByVal
p_intData As Integer) As String
print("fn5 was called with parameters : " &
p_strData & " AND " & p_intData & ". Returning a
concatenated value.")
Return p_strData & p_intData
End Function

Public Sub print(ByVal p_objObject As Object)
Console.WriteLine(p_objObject)
End Sub

Hope this helps

regards,

sr
-----Original Message-----
I have seen this described so that a function from another assembly can beloaded, but I know the function will be in the compiled assembly that runsthe main program. I am still searching for somebody that has coded this . ...

Any ideas?

Derek
"Mattias Sjögren" <ma********************@mvps.org> wrote in messagenews:%2****************@TK2MSFTNGP11.phx.gbl...
Derek,
>I have seen a ton of people discuss how reflection does this, but I
cannot
>find the syntax to do this. I have tried several code
example off of >gotdotnet and other articles. Can somebody please show me the code to do >this?


There's no really easy way to do this. You'll have to

parse out the function name and parameters, look up the method you want to call, convert the parameter types if needed, and then call the function.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

.

Nov 12 '05 #4
Hello Derek,

Here are many code samples on how to invoke different kinds of methods
through reflection. In the samples, because the names of the methods being
invoked are stored in strings, this mechanism provides the capability to
specify methods to be invoked at runtime, rather than at design-time.

http://samples.gotdotnet.com/quickst...oc/Invoke.aspx

Hope it helps.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com
--------------------
!From: "Derek Hart" <dm****@gte.net>
!References: <#c*************@tk2msftngp13.phx.gbl>
<#M**************@TK2MSFTNGP11.phx.gbl>
<ee**************@tk2msftngp13.phx.gbl>
<03****************************@phx.gbl>
!Subject: Re: Reflection - Run Function From String
!Date: Tue, 8 Jul 2003 10:22:46 -0700
!Lines: 163
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <OQ**************@TK2MSFTNGP11.phx.gbl>
!Newsgroups: microsoft.public.dotnet.general
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:14973
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Hi, thank you for responding. When I paste the code in there are a ton of
!errors. Should I paste this into a global module? Can you email me this
in
!a module or a VB.Net app? Thank you so much for responding. Nobody has
!been able to answer this.
!
!
!"SR" <rs*****@hotmail.com> wrote in message
!news:03****************************@phx.gbl...
!Hi
!
!Is ur reqmt that "A Function Name will be passed at
!runtime and you need to execute that function using
!reflection?".Please confirm...If that is the case here u go
!
!The fnExecuteMethods takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThruReflection()
!
! 'No Parameters
! fnExecuteMethods(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethods(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethods(Me, "fn3", l_objParams3)
! 'One String Parameter and One Integer Parameter
! Dim l_objParams4(1) As Object
! l_objParams4(0) = "Hi"
! l_objParams4(1) = 20
! fnExecuteMethods(Me, "fn4", l_objParams4)
! 'One String Parameter and One Integer Parameter
!and One String as return Parameter
! Dim l_objParams5(1) As Object
! l_objParams5(0) = "Hi"
! l_objParams5(1) = 20
! Dim l_strRetVal As String = fnExecuteMethods
!(Me, "fn5", l_objParams5).ToString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethods(ByVal p_objObject As
!Object, ByVal p_objMethodName As String, Optional ByVal
!p_objParams() As Object = Nothing) As Object
!
! ' Create a type object and store the type of the
!object passed
! Dim l_objType As Type = p_objObject.GetType
! 'Declare a MethodInfo object to store the Methods
!of the class
! Dim l_objMethodInfo As MethodInfo
! 'Declare a variable to loop through the Methods of
!this class
! Dim l_intMethodCtr As Integer
!
! ' Get the MethodInfo for the current class.
!Binding Flags are specified to get the
! ' public and private Methods of this class. When
!Public or Non-Public is specified
! ' in the BindingFlags, it is also necessary to
!specify Static or Instance
! l_objMethodInfo = l_objType.GetMethod
!(p_objMethodName, BindingFlags.NonPublic Or
!BindingFlags.Public Or BindingFlags.Static Or
!BindingFlags.Instance)
!
! Return l_objMethodInfo.Invoke(p_objObject,
!p_objParams)
!
! End Function
!
! Public Sub fn1()
! print("fn1 was called")
! End Sub
!
! Public Sub fn2(ByVal p_strData As String)
! print("fn2 was called with parameter : " &
!p_strData)
! End Sub
!
! Public Sub fn3(ByVal p_intData As Integer)
! print("fn3 was called with parameter : " &
!p_intData)
! End Sub
!
! Public Sub fn4(ByVal p_strData As String, ByVal
!p_intData As Integer)
! print("fn4 was called with parameters : " &
!p_strData & " AND " & p_intData)
! End Sub
!
! Public Function fn5(ByVal p_strData As String, ByVal
!p_intData As Integer) As String
! print("fn5 was called with parameters : " &
!p_strData & " AND " & p_intData & ". Returning a
!concatenated value.")
! Return p_strData & p_intData
! End Function
!
! Public Sub print(ByVal p_objObject As Object)
! Console.WriteLine(p_objObject)
! End Sub
!
!Hope this helps
!
!regards,
!
!sr
!
!>-----Original Message-----
!>I have seen this described so that a function from
!another assembly can be
!>loaded, but I know the function will be in the compiled
!assembly that runs
!>the main program. I am still searching for somebody that
!has coded this . .
!>..
!>
!>Any ideas?
!>
!>Derek
!>
!>
!>"Mattias Sjögren" <ma********************@mvps.org> wrote
!in message
!>news:%2****************@TK2MSFTNGP11.phx.gbl...
!>> Derek,
!>>
!>> >I have seen a ton of people discuss how reflection
!does this, but I
!>cannot
!>> >find the syntax to do this. I have tried several code
!example off of
!>> >gotdotnet and other articles. Can somebody please
!show me the code to do
!>> >this?
!>>
!>> There's no really easy way to do this. You'll have to
!parse out the
!>> function name and parameters, look up the method you
!want to call,
!>> convert the parameter types if needed, and then call
!the function.
!>>
!>>
!>>
!>> Mattias
!>>
!>> --
!>> Mattias Sjögren [MVP] mattias @ mvps.org
!>> http://www.msjogren.net/dotnet/
!>> Please reply only to the newsgroup.
!>
!>
!>.
!>
!
!
!

Nov 12 '05 #5
Yes, I have looked at this. I bring in the first routine, and there are
errors in it that I just don't understand. I wish I could find clean
example of this . . .

Derek

"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
news:nM**************@cpmsftngxa09.phx.gbl...
Hello Derek,

Here are many code samples on how to invoke different kinds of methods
through reflection. In the samples, because the names of the methods being
invoked are stored in strings, this mechanism provides the capability to
specify methods to be invoked at runtime, rather than at design-time.

http://samples.gotdotnet.com/quickst...oc/Invoke.aspx

Hope it helps.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights. Got .Net? http://www.gotdotnet.com
--------------------
!From: "Derek Hart" <dm****@gte.net>
!References: <#c*************@tk2msftngp13.phx.gbl>
<#M**************@TK2MSFTNGP11.phx.gbl>
<ee**************@tk2msftngp13.phx.gbl>
<03****************************@phx.gbl>
!Subject: Re: Reflection - Run Function From String
!Date: Tue, 8 Jul 2003 10:22:46 -0700
!Lines: 163
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <OQ**************@TK2MSFTNGP11.phx.gbl>
!Newsgroups: microsoft.public.dotnet.general
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:14973
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Hi, thank you for responding. When I paste the code in there are a ton of
!errors. Should I paste this into a global module? Can you email me this
in
!a module or a VB.Net app? Thank you so much for responding. Nobody has
!been able to answer this.
!
!
!"SR" <rs*****@hotmail.com> wrote in message
!news:03****************************@phx.gbl...
!Hi
!
!Is ur reqmt that "A Function Name will be passed at
!runtime and you need to execute that function using
!reflection?".Please confirm...If that is the case here u go
!
!The fnExecuteMethods takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThruReflection()
!
! 'No Parameters
! fnExecuteMethods(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethods(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethods(Me, "fn3", l_objParams3)
! 'One String Parameter and One Integer Parameter
! Dim l_objParams4(1) As Object
! l_objParams4(0) = "Hi"
! l_objParams4(1) = 20
! fnExecuteMethods(Me, "fn4", l_objParams4)
! 'One String Parameter and One Integer Parameter
!and One String as return Parameter
! Dim l_objParams5(1) As Object
! l_objParams5(0) = "Hi"
! l_objParams5(1) = 20
! Dim l_strRetVal As String = fnExecuteMethods
!(Me, "fn5", l_objParams5).ToString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethods(ByVal p_objObject As
!Object, ByVal p_objMethodName As String, Optional ByVal
!p_objParams() As Object = Nothing) As Object
!
! ' Create a type object and store the type of the
!object passed
! Dim l_objType As Type = p_objObject.GetType
! 'Declare a MethodInfo object to store the Methods
!of the class
! Dim l_objMethodInfo As MethodInfo
! 'Declare a variable to loop through the Methods of
!this class
! Dim l_intMethodCtr As Integer
!
! ' Get the MethodInfo for the current class.
!Binding Flags are specified to get the
! ' public and private Methods of this class. When
!Public or Non-Public is specified
! ' in the BindingFlags, it is also necessary to
!specify Static or Instance
! l_objMethodInfo = l_objType.GetMethod
!(p_objMethodName, BindingFlags.NonPublic Or
!BindingFlags.Public Or BindingFlags.Static Or
!BindingFlags.Instance)
!
! Return l_objMethodInfo.Invoke(p_objObject,
!p_objParams)
!
! End Function
!
! Public Sub fn1()
! print("fn1 was called")
! End Sub
!
! Public Sub fn2(ByVal p_strData As String)
! print("fn2 was called with parameter : " &
!p_strData)
! End Sub
!
! Public Sub fn3(ByVal p_intData As Integer)
! print("fn3 was called with parameter : " &
!p_intData)
! End Sub
!
! Public Sub fn4(ByVal p_strData As String, ByVal
!p_intData As Integer)
! print("fn4 was called with parameters : " &
!p_strData & " AND " & p_intData)
! End Sub
!
! Public Function fn5(ByVal p_strData As String, ByVal
!p_intData As Integer) As String
! print("fn5 was called with parameters : " &
!p_strData & " AND " & p_intData & ". Returning a
!concatenated value.")
! Return p_strData & p_intData
! End Function
!
! Public Sub print(ByVal p_objObject As Object)
! Console.WriteLine(p_objObject)
! End Sub
!
!Hope this helps
!
!regards,
!
!sr
!
!>-----Original Message-----
!>I have seen this described so that a function from
!another assembly can be
!>loaded, but I know the function will be in the compiled
!assembly that runs
!>the main program. I am still searching for somebody that
!has coded this . .
!>..
!>
!>Any ideas?
!>
!>Derek
!>
!>
!>"Mattias Sjögren" <ma********************@mvps.org> wrote
!in message
!>news:%2****************@TK2MSFTNGP11.phx.gbl...
!>> Derek,
!>>
!>> >I have seen a ton of people discuss how reflection
!does this, but I
!>cannot
!>> >find the syntax to do this. I have tried several code
!example off of
!>> >gotdotnet and other articles. Can somebody please
!show me the code to do
!>> >this?
!>>
!>> There's no really easy way to do this. You'll have to
!parse out the
!>> function name and parameters, look up the method you
!want to call,
!>> convert the parameter types if needed, and then call
!the function.
!>>
!>>
!>>
!>> Mattias
!>>
!>> --
!>> Mattias Sjögren [MVP] mattias @ mvps.org
!>> http://www.msjogren.net/dotnet/
!>> Please reply only to the newsgroup.
!>
!>
!>.
!>
!
!
!

Nov 12 '05 #6
Hello Derek,

Could you please post the code slice that you used here? The people here
should be able to give you some idea. :)

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com
--------------------
!From: "Derek Hart" <dm****@gte.net>
!References: <#c*************@tk2msftngp13.phx.gbl>
<#M**************@TK2MSFTNGP11.phx.gbl>
<ee**************@tk2msftngp13.phx.gbl>
<03****************************@phx.gbl>
<OQ**************@TK2MSFTNGP11.phx.gbl>
<nM**************@cpmsftngxa09.phx.gbl>
!Subject: Re: Reflection - Run Function From String
!Date: Tue, 8 Jul 2003 20:13:04 -0700
!Lines: 216
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <eT**************@TK2MSFTNGP10.phx.gbl>
!Newsgroups: microsoft.public.dotnet.general
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
!Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:15009
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Yes, I have looked at this. I bring in the first routine, and there are
!errors in it that I just don't understand. I wish I could find clean
!example of this . . .
!
!Derek
!
!"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
!news:nM**************@cpmsftngxa09.phx.gbl...
!> Hello Derek,
!>
!> Here are many code samples on how to invoke different kinds of methods
!> through reflection. In the samples, because the names of the methods
being
!> invoked are stored in strings, this mechanism provides the capability to
!> specify methods to be invoked at runtime, rather than at design-time.
!>
!> http://samples.gotdotnet.com/quickst...oc/Invoke.aspx
!>
!> Hope it helps.
!>
!> Best regards,
!> yhhuang
!> VS.NET, Visual C++
!> Microsoft
!>
!> This posting is provided "AS IS" with no warranties, and confers no
!rights.
!> Got .Net? http://www.gotdotnet.com
!> --------------------
!> !From: "Derek Hart" <dm****@gte.net>
!> !References: <#c*************@tk2msftngp13.phx.gbl>
!> <#M**************@TK2MSFTNGP11.phx.gbl>
!> <ee**************@tk2msftngp13.phx.gbl>
!> <03****************************@phx.gbl>
!> !Subject: Re: Reflection - Run Function From String
!> !Date: Tue, 8 Jul 2003 10:22:46 -0700
!> !Lines: 163
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !Message-ID: <OQ**************@TK2MSFTNGP11.phx.gbl>
!> !Newsgroups: microsoft.public.dotnet.general
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!> !Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:14973
!> !X-Tomcat-NG: microsoft.public.dotnet.general
!> !
!> !Hi, thank you for responding. When I paste the code in there are a ton
of
!> !errors. Should I paste this into a global module? Can you email me
this
!> in
!> !a module or a VB.Net app? Thank you so much for responding. Nobody has
!> !been able to answer this.
!> !
!> !
!> !"SR" <rs*****@hotmail.com> wrote in message
!> !news:03****************************@phx.gbl...
!> !Hi
!> !
!> !Is ur reqmt that "A Function Name will be passed at
!> !runtime and you need to execute that function using
!> !reflection?".Please confirm...If that is the case here u go
!> !
!> !The fnExecuteMethods takes an object, a method name and
!> !optionally parameters and invokes those methods
!> !
!> !Public Sub CallMethodsThruReflection()
!> !
!> ! 'No Parameters
!> ! fnExecuteMethods(Me, "fn1")
!> ! 'One String Parameter
!> ! Dim l_objParams2(0) As Object
!> ! l_objParams2(0) = "Hi"
!> ! fnExecuteMethods(Me, "fn2", l_objParams2)
!> ! 'One Integer Parameter
!> ! Dim l_objParams3(0) As Object
!> ! l_objParams3(0) = 20
!> ! fnExecuteMethods(Me, "fn3", l_objParams3)
!> ! 'One String Parameter and One Integer Parameter
!> ! Dim l_objParams4(1) As Object
!> ! l_objParams4(0) = "Hi"
!> ! l_objParams4(1) = 20
!> ! fnExecuteMethods(Me, "fn4", l_objParams4)
!> ! 'One String Parameter and One Integer Parameter
!> !and One String as return Parameter
!> ! Dim l_objParams5(1) As Object
!> ! l_objParams5(0) = "Hi"
!> ! l_objParams5(1) = 20
!> ! Dim l_strRetVal As String = fnExecuteMethods
!> !(Me, "fn5", l_objParams5).ToString
!> ! print("Return Value from fn5 : " & l_strRetVal)
!> !
!> !
!> ! End Sub
!> !
!> ! Public Function fnExecuteMethods(ByVal p_objObject As
!> !Object, ByVal p_objMethodName As String, Optional ByVal
!> !p_objParams() As Object = Nothing) As Object
!> !
!> ! ' Create a type object and store the type of the
!> !object passed
!> ! Dim l_objType As Type = p_objObject.GetType
!> ! 'Declare a MethodInfo object to store the Methods
!> !of the class
!> ! Dim l_objMethodInfo As MethodInfo
!> ! 'Declare a variable to loop through the Methods of
!> !this class
!> ! Dim l_intMethodCtr As Integer
!> !
!> ! ' Get the MethodInfo for the current class.
!> !Binding Flags are specified to get the
!> ! ' public and private Methods of this class. When
!> !Public or Non-Public is specified
!> ! ' in the BindingFlags, it is also necessary to
!> !specify Static or Instance
!> ! l_objMethodInfo = l_objType.GetMethod
!> !(p_objMethodName, BindingFlags.NonPublic Or
!> !BindingFlags.Public Or BindingFlags.Static Or
!> !BindingFlags.Instance)
!> !
!> ! Return l_objMethodInfo.Invoke(p_objObject,
!> !p_objParams)
!> !
!> ! End Function
!> !
!> ! Public Sub fn1()
!> ! print("fn1 was called")
!> ! End Sub
!> !
!> ! Public Sub fn2(ByVal p_strData As String)
!> ! print("fn2 was called with parameter : " &
!> !p_strData)
!> ! End Sub
!> !
!> ! Public Sub fn3(ByVal p_intData As Integer)
!> ! print("fn3 was called with parameter : " &
!> !p_intData)
!> ! End Sub
!> !
!> ! Public Sub fn4(ByVal p_strData As String, ByVal
!> !p_intData As Integer)
!> ! print("fn4 was called with parameters : " &
!> !p_strData & " AND " & p_intData)
!> ! End Sub
!> !
!> ! Public Function fn5(ByVal p_strData As String, ByVal
!> !p_intData As Integer) As String
!> ! print("fn5 was called with parameters : " &
!> !p_strData & " AND " & p_intData & ". Returning a
!> !concatenated value.")
!> ! Return p_strData & p_intData
!> ! End Function
!> !
!> ! Public Sub print(ByVal p_objObject As Object)
!> ! Console.WriteLine(p_objObject)
!> ! End Sub
!> !
!> !Hope this helps
!> !
!> !regards,
!> !
!> !sr
!> !
!> !>-----Original Message-----
!> !>I have seen this described so that a function from
!> !another assembly can be
!> !>loaded, but I know the function will be in the compiled
!> !assembly that runs
!> !>the main program. I am still searching for somebody that
!> !has coded this . .
!> !>..
!> !>
!> !>Any ideas?
!> !>
!> !>Derek
!> !>
!> !>
!> !>"Mattias Sjögren" <ma********************@mvps.org> wrote
!> !in message
!> !>news:%2****************@TK2MSFTNGP11.phx.gbl...
!> !>> Derek,
!> !>>
!> !>> >I have seen a ton of people discuss how reflection
!> !does this, but I
!> !>cannot
!> !>> >find the syntax to do this. I have tried several code
!> !example off of
!> !>> >gotdotnet and other articles. Can somebody please
!> !show me the code to do
!> !>> >this?
!> !>>
!> !>> There's no really easy way to do this. You'll have to
!> !parse out the
!> !>> function name and parameters, look up the method you
!> !want to call,
!> !>> convert the parameter types if needed, and then call
!> !the function.
!> !>>
!> !>>
!> !>>
!> !>> Mattias
!> !>>
!> !>> --
!> !>> Mattias Sjögren [MVP] mattias @ mvps.org
!> !>> http://www.msjogren.net/dotnet/
!> !>> Please reply only to the newsgroup.
!> !>
!> !>
!> !>.
!> !>
!> !
!> !
!> !
!>
!
!
!

Nov 12 '05 #7
OK, here is the code slice:

The fnExecuteMethods takes an object, a method name and
optionally parameters and invokes those methods

Public Sub CallMethodsThruReflection()

'No Parameters
fnExecuteMethods(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethods(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethods(Me, "fn3", l_objParams3)
'One String Parameter and One Integer Parameter
Dim l_objParams4(1) As Object
l_objParams4(0) = "Hi"
l_objParams4(1) = 20
fnExecuteMethods(Me, "fn4", l_objParams4)
'One String Parameter and One Integer Parameter
and One String as return Parameter
Dim l_objParams5(1) As Object
l_objParams5(0) = "Hi"
l_objParams5(1) = 20
Dim l_strRetVal As String = fnExecuteMethods
(Me, "fn5", l_objParams5).ToString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethods(ByVal p_objObject As
Object, ByVal p_objMethodName As String, Optional ByVal
p_objParams() As Object = Nothing) As Object

' Create a type object and store the type of the
object passed
Dim l_objType As Type = p_objObject.GetType
'Declare a MethodInfo object to store the Methods
of the class
Dim l_objMethodInfo As MethodInfo
'Declare a variable to loop through the Methods of
this class
Dim l_intMethodCtr As Integer

' Get the MethodInfo for the current class.
Binding Flags are specified to get the
' public and private Methods of this class. When
Public or Non-Public is specified
' in the BindingFlags, it is also necessary to
specify Static or Instance
l_objMethodInfo = l_objType.GetMethod
(p_objMethodName, BindingFlags.NonPublic Or
BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)

Return l_objMethodInfo.Invoke(p_objObject,
p_objParams)

End Function

Public Sub fn1()
print("fn1 was called")
End Sub

Public Sub fn2(ByVal p_strData As String)
print("fn2 was called with parameter : " &
p_strData)
End Sub

Public Sub fn3(ByVal p_intData As Integer)
print("fn3 was called with parameter : " &
p_intData)
End Sub

Public Sub fn4(ByVal p_strData As String, ByVal
p_intData As Integer)
print("fn4 was called with parameters : " &
p_strData & " AND " & p_intData)
End Sub

Public Function fn5(ByVal p_strData As String, ByVal
p_intData As Integer) As String
print("fn5 was called with parameters : " &
p_strData & " AND " & p_intData & ". Returning a
concatenated value.")
Return p_strData & p_intData
End Function

Public Sub print(ByVal p_objObject As Object)
Console.WriteLine(p_objObject)
End Sub

Derek
"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
news:bm**************@cpmsftngxa09.phx.gbl...
Hello Derek,

Could you please post the code slice that you used here? The people here
should be able to give you some idea. :)

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights. Got .Net? http://www.gotdotnet.com
--------------------
!From: "Derek Hart" <dm****@gte.net>
!References: <#c*************@tk2msftngp13.phx.gbl>
<#M**************@TK2MSFTNGP11.phx.gbl>
<ee**************@tk2msftngp13.phx.gbl>
<03****************************@phx.gbl>
<OQ**************@TK2MSFTNGP11.phx.gbl>
<nM**************@cpmsftngxa09.phx.gbl>
!Subject: Re: Reflection - Run Function From String
!Date: Tue, 8 Jul 2003 20:13:04 -0700
!Lines: 216
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <eT**************@TK2MSFTNGP10.phx.gbl>
!Newsgroups: microsoft.public.dotnet.general
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
!Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:15009
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!Yes, I have looked at this. I bring in the first routine, and there are
!errors in it that I just don't understand. I wish I could find clean
!example of this . . .
!
!Derek
!
!"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
!news:nM**************@cpmsftngxa09.phx.gbl...
!> Hello Derek,
!>
!> Here are many code samples on how to invoke different kinds of methods
!> through reflection. In the samples, because the names of the methods
being
!> invoked are stored in strings, this mechanism provides the capability to !> specify methods to be invoked at runtime, rather than at design-time.
!>
!> http://samples.gotdotnet.com/quickst...oc/Invoke.aspx
!>
!> Hope it helps.
!>
!> Best regards,
!> yhhuang
!> VS.NET, Visual C++
!> Microsoft
!>
!> This posting is provided "AS IS" with no warranties, and confers no
!rights.
!> Got .Net? http://www.gotdotnet.com
!> --------------------
!> !From: "Derek Hart" <dm****@gte.net>
!> !References: <#c*************@tk2msftngp13.phx.gbl>
!> <#M**************@TK2MSFTNGP11.phx.gbl>
!> <ee**************@tk2msftngp13.phx.gbl>
!> <03****************************@phx.gbl>
!> !Subject: Re: Reflection - Run Function From String
!> !Date: Tue, 8 Jul 2003 10:22:46 -0700
!> !Lines: 163
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !Message-ID: <OQ**************@TK2MSFTNGP11.phx.gbl>
!> !Newsgroups: microsoft.public.dotnet.general
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!> !Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:14973
!> !X-Tomcat-NG: microsoft.public.dotnet.general
!> !
!> !Hi, thank you for responding. When I paste the code in there are a ton
of
!> !errors. Should I paste this into a global module? Can you email me
this
!> in
!> !a module or a VB.Net app? Thank you so much for responding. Nobody has !> !been able to answer this.
!> !
!> !
!> !"SR" <rs*****@hotmail.com> wrote in message
!> !news:03****************************@phx.gbl...
!> !Hi
!> !
!> !Is ur reqmt that "A Function Name will be passed at
!> !runtime and you need to execute that function using
!> !reflection?".Please confirm...If that is the case here u go
!> !
!> !The fnExecuteMethods takes an object, a method name and
!> !optionally parameters and invokes those methods
!> !
!> !Public Sub CallMethodsThruReflection()
!> !
!> ! 'No Parameters
!> ! fnExecuteMethods(Me, "fn1")
!> ! 'One String Parameter
!> ! Dim l_objParams2(0) As Object
!> ! l_objParams2(0) = "Hi"
!> ! fnExecuteMethods(Me, "fn2", l_objParams2)
!> ! 'One Integer Parameter
!> ! Dim l_objParams3(0) As Object
!> ! l_objParams3(0) = 20
!> ! fnExecuteMethods(Me, "fn3", l_objParams3)
!> ! 'One String Parameter and One Integer Parameter
!> ! Dim l_objParams4(1) As Object
!> ! l_objParams4(0) = "Hi"
!> ! l_objParams4(1) = 20
!> ! fnExecuteMethods(Me, "fn4", l_objParams4)
!> ! 'One String Parameter and One Integer Parameter
!> !and One String as return Parameter
!> ! Dim l_objParams5(1) As Object
!> ! l_objParams5(0) = "Hi"
!> ! l_objParams5(1) = 20
!> ! Dim l_strRetVal As String = fnExecuteMethods
!> !(Me, "fn5", l_objParams5).ToString
!> ! print("Return Value from fn5 : " & l_strRetVal)
!> !
!> !
!> ! End Sub
!> !
!> ! Public Function fnExecuteMethods(ByVal p_objObject As
!> !Object, ByVal p_objMethodName As String, Optional ByVal
!> !p_objParams() As Object = Nothing) As Object
!> !
!> ! ' Create a type object and store the type of the
!> !object passed
!> ! Dim l_objType As Type = p_objObject.GetType
!> ! 'Declare a MethodInfo object to store the Methods
!> !of the class
!> ! Dim l_objMethodInfo As MethodInfo
!> ! 'Declare a variable to loop through the Methods of
!> !this class
!> ! Dim l_intMethodCtr As Integer
!> !
!> ! ' Get the MethodInfo for the current class.
!> !Binding Flags are specified to get the
!> ! ' public and private Methods of this class. When
!> !Public or Non-Public is specified
!> ! ' in the BindingFlags, it is also necessary to
!> !specify Static or Instance
!> ! l_objMethodInfo = l_objType.GetMethod
!> !(p_objMethodName, BindingFlags.NonPublic Or
!> !BindingFlags.Public Or BindingFlags.Static Or
!> !BindingFlags.Instance)
!> !
!> ! Return l_objMethodInfo.Invoke(p_objObject,
!> !p_objParams)
!> !
!> ! End Function
!> !
!> ! Public Sub fn1()
!> ! print("fn1 was called")
!> ! End Sub
!> !
!> ! Public Sub fn2(ByVal p_strData As String)
!> ! print("fn2 was called with parameter : " &
!> !p_strData)
!> ! End Sub
!> !
!> ! Public Sub fn3(ByVal p_intData As Integer)
!> ! print("fn3 was called with parameter : " &
!> !p_intData)
!> ! End Sub
!> !
!> ! Public Sub fn4(ByVal p_strData As String, ByVal
!> !p_intData As Integer)
!> ! print("fn4 was called with parameters : " &
!> !p_strData & " AND " & p_intData)
!> ! End Sub
!> !
!> ! Public Function fn5(ByVal p_strData As String, ByVal
!> !p_intData As Integer) As String
!> ! print("fn5 was called with parameters : " &
!> !p_strData & " AND " & p_intData & ". Returning a
!> !concatenated value.")
!> ! Return p_strData & p_intData
!> ! End Function
!> !
!> ! Public Sub print(ByVal p_objObject As Object)
!> ! Console.WriteLine(p_objObject)
!> ! End Sub
!> !
!> !Hope this helps
!> !
!> !regards,
!> !
!> !sr
!> !
!> !>-----Original Message-----
!> !>I have seen this described so that a function from
!> !another assembly can be
!> !>loaded, but I know the function will be in the compiled
!> !assembly that runs
!> !>the main program. I am still searching for somebody that
!> !has coded this . .
!> !>..
!> !>
!> !>Any ideas?
!> !>
!> !>Derek
!> !>
!> !>
!> !>"Mattias Sjögren" <ma********************@mvps.org> wrote
!> !in message
!> !>news:%2****************@TK2MSFTNGP11.phx.gbl...
!> !>> Derek,
!> !>>
!> !>> >I have seen a ton of people discuss how reflection
!> !does this, but I
!> !>cannot
!> !>> >find the syntax to do this. I have tried several code
!> !example off of
!> !>> >gotdotnet and other articles. Can somebody please
!> !show me the code to do
!> !>> >this?
!> !>>
!> !>> There's no really easy way to do this. You'll have to
!> !parse out the
!> !>> function name and parameters, look up the method you
!> !want to call,
!> !>> convert the parameter types if needed, and then call
!> !the function.
!> !>>
!> !>>
!> !>>
!> !>> Mattias
!> !>>
!> !>> --
!> !>> Mattias Sjögren [MVP] mattias @ mvps.org
!> !>> http://www.msjogren.net/dotnet/
!> !>> Please reply only to the newsgroup.
!> !>
!> !>
!> !>.
!> !>
!> !
!> !
!> !
!>
!
!
!

Nov 12 '05 #8
Hello Derek,

Sorry I am pretty busy recently. I will look into it and reply you ASAP. Thanks very much.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com
--------------------
!From: "Derek Hart" <dm****@gte.net>
!References: <#c*************@tk2msftngp13.phx.gbl> <#M**************@TK2MSFTNGP11.phx.gbl>
<ee**************@tk2msftngp13.phx.gbl> <03****************************@phx.gbl> <OQkLQWXRDHA.2144
@TK2MSFTNGP11.phx.gbl> <nM**************@cpmsftngxa09.phx.gbl> <eTgAGgcRDHA.2148
@TK2MSFTNGP10.phx.gbl> <bm**************@cpmsftngxa09.phx.gbl>
!Subject: Re: Reflection - Run Function From String
!Date: Wed, 9 Jul 2003 08:52:49 -0700
!Lines: 364
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <#I**************@TK2MSFTNGP12.phx.gbl>
!Newsgroups: microsoft.public.dotnet.general
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net 4.60.38.89
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:100650
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!OK, here is the code slice:
!
!The fnExecuteMethods takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThruReflection()
!
! 'No Parameters
! fnExecuteMethods(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethods(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethods(Me, "fn3", l_objParams3)
! 'One String Parameter and One Integer Parameter
! Dim l_objParams4(1) As Object
! l_objParams4(0) = "Hi"
! l_objParams4(1) = 20
! fnExecuteMethods(Me, "fn4", l_objParams4)
! 'One String Parameter and One Integer Parameter
!and One String as return Parameter
! Dim l_objParams5(1) As Object
! l_objParams5(0) = "Hi"
! l_objParams5(1) = 20
! Dim l_strRetVal As String = fnExecuteMethods
!(Me, "fn5", l_objParams5).ToString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethods(ByVal p_objObject As
!Object, ByVal p_objMethodName As String, Optional ByVal
!p_objParams() As Object = Nothing) As Object
!
! ' Create a type object and store the type of the
!object passed
! Dim l_objType As Type = p_objObject.GetType
! 'Declare a MethodInfo object to store the Methods
!of the class
! Dim l_objMethodInfo As MethodInfo
! 'Declare a variable to loop through the Methods of
!this class
! Dim l_intMethodCtr As Integer
!
! ' Get the MethodInfo for the current class.
!Binding Flags are specified to get the
! ' public and private Methods of this class. When
!Public or Non-Public is specified
! ' in the BindingFlags, it is also necessary to
!specify Static or Instance
! l_objMethodInfo = l_objType.GetMethod
!(p_objMethodName, BindingFlags.NonPublic Or
!BindingFlags.Public Or BindingFlags.Static Or
!BindingFlags.Instance)
!
! Return l_objMethodInfo.Invoke(p_objObject,
!p_objParams)
!
! End Function
!
! Public Sub fn1()
! print("fn1 was called")
! End Sub
!
! Public Sub fn2(ByVal p_strData As String)
! print("fn2 was called with parameter : " &
!p_strData)
! End Sub
!
! Public Sub fn3(ByVal p_intData As Integer)
! print("fn3 was called with parameter : " &
!p_intData)
! End Sub
!
! Public Sub fn4(ByVal p_strData As String, ByVal
!p_intData As Integer)
! print("fn4 was called with parameters : " &
!p_strData & " AND " & p_intData)
! End Sub
!
! Public Function fn5(ByVal p_strData As String, ByVal
!p_intData As Integer) As String
! print("fn5 was called with parameters : " &
!p_strData & " AND " & p_intData & ". Returning a
!concatenated value.")
! Return p_strData & p_intData
! End Function
!
! Public Sub print(ByVal p_objObject As Object)
! Console.WriteLine(p_objObject)
! End Sub
!
!
!
!Derek
!
!
!"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
!news:bm**************@cpmsftngxa09.phx.gbl...
!> Hello Derek,
!>
!> Could you please post the code slice that you used here? The people here
!> should be able to give you some idea. :)
!>
!> Best regards,
!> yhhuang
!> VS.NET, Visual C++
!> Microsoft
!>
!> This posting is provided "AS IS" with no warranties, and confers no
!rights.
!> Got .Net? http://www.gotdotnet.com
!> --------------------
!> !From: "Derek Hart" <dm****@gte.net>
!> !References: <#c*************@tk2msftngp13.phx.gbl>
!> <#M**************@TK2MSFTNGP11.phx.gbl>
!> <ee**************@tk2msftngp13.phx.gbl>
!> <03****************************@phx.gbl>
!> <OQ**************@TK2MSFTNGP11.phx.gbl>
!> <nM**************@cpmsftngxa09.phx.gbl>
!> !Subject: Re: Reflection - Run Function From String
!> !Date: Tue, 8 Jul 2003 20:13:04 -0700
!> !Lines: 216
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !Message-ID: <eT**************@TK2MSFTNGP10.phx.gbl>
!> !Newsgroups: microsoft.public.dotnet.general
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
!> !Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:15009
!> !X-Tomcat-NG: microsoft.public.dotnet.general
!> !
!> !Yes, I have looked at this. I bring in the first routine, and there are
!> !errors in it that I just don't understand. I wish I could find clean
!> !example of this . . .
!> !
!> !Derek
!> !
!> !"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
!> !news:nM**************@cpmsftngxa09.phx.gbl...
!> !> Hello Derek,
!> !>
!> !> Here are many code samples on how to invoke different kinds of methods
!> !> through reflection. In the samples, because the names of the methods
!> being
!> !> invoked are stored in strings, this mechanism provides the capability
!to
!> !> specify methods to be invoked at runtime, rather than at design-time.
!> !>
!> !> http://samples.gotdotnet.com/quickst...oc/Invoke.aspx
!> !>
!> !> Hope it helps.
!> !>
!> !> Best regards,
!> !> yhhuang
!> !> VS.NET, Visual C++
!> !> Microsoft
!> !>
!> !> This posting is provided "AS IS" with no warranties, and confers no
!> !rights.
!> !> Got .Net? http://www.gotdotnet.com
!> !> --------------------
!> !> !From: "Derek Hart" <dm****@gte.net>
!> !> !References: <#c*************@tk2msftngp13.phx.gbl>
!> !> <#M**************@TK2MSFTNGP11.phx.gbl>
!> !> <ee**************@tk2msftngp13.phx.gbl>
!> !> <03****************************@phx.gbl>
!> !> !Subject: Re: Reflection - Run Function From String
!> !> !Date: Tue, 8 Jul 2003 10:22:46 -0700
!> !> !Lines: 163
!> !> !X-Priority: 3
!> !> !X-MSMail-Priority: Normal
!> !> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !> !Message-ID: <OQ**************@TK2MSFTNGP11.phx.gbl>
!> !> !Newsgroups: microsoft.public.dotnet.general
!> !> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> !> 4.60.38.89
!> !> !Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!> !> !Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:14973
!> !> !X-Tomcat-NG: microsoft.public.dotnet.general
!> !> !
!> !> !Hi, thank you for responding. When I paste the code in there are a ton
!> of
!> !> !errors. Should I paste this into a global module? Can you email me
!> this
!> !> in
!> !> !a module or a VB.Net app? Thank you so much for responding. Nobody
!has
!> !> !been able to answer this.
!> !> !
!> !> !
!> !> !"SR" <rs*****@hotmail.com> wrote in message
!> !> !news:03****************************@phx.gbl...
!> !> !Hi
!> !> !
!> !> !Is ur reqmt that "A Function Name will be passed at
!> !> !runtime and you need to execute that function using
!> !> !reflection?".Please confirm...If that is the case here u go
!> !> !
!> !> !The fnExecuteMethods takes an object, a method name and
!> !> !optionally parameters and invokes those methods
!> !> !
!> !> !Public Sub CallMethodsThruReflection()
!> !> !
!> !> ! 'No Parameters
!> !> ! fnExecuteMethods(Me, "fn1")
!> !> ! 'One String Parameter
!> !> ! Dim l_objParams2(0) As Object
!> !> ! l_objParams2(0) = "Hi"
!> !> ! fnExecuteMethods(Me, "fn2", l_objParams2)
!> !> ! 'One Integer Parameter
!> !> ! Dim l_objParams3(0) As Object
!> !> ! l_objParams3(0) = 20
!> !> ! fnExecuteMethods(Me, "fn3", l_objParams3)
!> !> ! 'One String Parameter and One Integer Parameter
!> !> ! Dim l_objParams4(1) As Object
!> !> ! l_objParams4(0) = "Hi"
!> !> ! l_objParams4(1) = 20
!> !> ! fnExecuteMethods(Me, "fn4", l_objParams4)
!> !> ! 'One String Parameter and One Integer Parameter
!> !> !and One String as return Parameter
!> !> ! Dim l_objParams5(1) As Object
!> !> ! l_objParams5(0) = "Hi"
!> !> ! l_objParams5(1) = 20
!> !> ! Dim l_strRetVal As String = fnExecuteMethods
!> !> !(Me, "fn5", l_objParams5).ToString
!> !> ! print("Return Value from fn5 : " & l_strRetVal)
!> !> !
!> !> !
!> !> ! End Sub
!> !> !
!> !> ! Public Function fnExecuteMethods(ByVal p_objObject As
!> !> !Object, ByVal p_objMethodName As String, Optional ByVal
!> !> !p_objParams() As Object = Nothing) As Object
!> !> !
!> !> ! ' Create a type object and store the type of the
!> !> !object passed
!> !> ! Dim l_objType As Type = p_objObject.GetType
!> !> ! 'Declare a MethodInfo object to store the Methods
!> !> !of the class
!> !> ! Dim l_objMethodInfo As MethodInfo
!> !> ! 'Declare a variable to loop through the Methods of
!> !> !this class
!> !> ! Dim l_intMethodCtr As Integer
!> !> !
!> !> ! ' Get the MethodInfo for the current class.
!> !> !Binding Flags are specified to get the
!> !> ! ' public and private Methods of this class. When
!> !> !Public or Non-Public is specified
!> !> ! ' in the BindingFlags, it is also necessary to
!> !> !specify Static or Instance
!> !> ! l_objMethodInfo = l_objType.GetMethod
!> !> !(p_objMethodName, BindingFlags.NonPublic Or
!> !> !BindingFlags.Public Or BindingFlags.Static Or
!> !> !BindingFlags.Instance)
!> !> !
!> !> ! Return l_objMethodInfo.Invoke(p_objObject,
!> !> !p_objParams)
!> !> !
!> !> ! End Function
!> !> !
!> !> ! Public Sub fn1()
!> !> ! print("fn1 was called")
!> !> ! End Sub
!> !> !
!> !> ! Public Sub fn2(ByVal p_strData As String)
!> !> ! print("fn2 was called with parameter : " &
!> !> !p_strData)
!> !> ! End Sub
!> !> !
!> !> ! Public Sub fn3(ByVal p_intData As Integer)
!> !> ! print("fn3 was called with parameter : " &
!> !> !p_intData)
!> !> ! End Sub
!> !> !
!> !> ! Public Sub fn4(ByVal p_strData As String, ByVal
!> !> !p_intData As Integer)
!> !> ! print("fn4 was called with parameters : " &
!> !> !p_strData & " AND " & p_intData)
!> !> ! End Sub
!> !> !
!> !> ! Public Function fn5(ByVal p_strData As String, ByVal
!> !> !p_intData As Integer) As String
!> !> ! print("fn5 was called with parameters : " &
!> !> !p_strData & " AND " & p_intData & ". Returning a
!> !> !concatenated value.")
!> !> ! Return p_strData & p_intData
!> !> ! End Function
!> !> !
!> !> ! Public Sub print(ByVal p_objObject As Object)
!> !> ! Console.WriteLine(p_objObject)
!> !> ! End Sub
!> !> !
!> !> !Hope this helps
!> !> !
!> !> !regards,
!> !> !
!> !> !sr
!> !> !
!> !> !>-----Original Message-----
!> !> !>I have seen this described so that a function from
!> !> !another assembly can be
!> !> !>loaded, but I know the function will be in the compiled
!> !> !assembly that runs
!> !> !>the main program. I am still searching for somebody that
!> !> !has coded this . .
!> !> !>..
!> !> !>
!> !> !>Any ideas?
!> !> !>
!> !> !>Derek
!> !> !>
!> !> !>
!> !> !>"Mattias Sjögren" <ma********************@mvps.org> wrote
!> !> !in message
!> !> !>news:%2****************@TK2MSFTNGP11.phx.gbl...
!> !> !>> Derek,
!> !> !>>
!> !> !>> >I have seen a ton of people discuss how reflection
!> !> !does this, but I
!> !> !>cannot
!> !> !>> >find the syntax to do this. I have tried several code
!> !> !example off of
!> !> !>> >gotdotnet and other articles. Can somebody please
!> !> !show me the code to do
!> !> !>> >this?
!> !> !>>
!> !> !>> There's no really easy way to do this. You'll have to
!> !> !parse out the
!> !> !>> function name and parameters, look up the method you
!> !> !want to call,
!> !> !>> convert the parameter types if needed, and then call
!> !> !the function.
!> !> !>>
!> !> !>>
!> !> !>>
!> !> !>> Mattias
!> !> !>>
!> !> !>> --
!> !> !>> Mattias Sjögren [MVP] mattias @ mvps.org
!> !> !>> http://www.msjogren.net/dotnet/
!> !> !>> Please reply only to the newsgroup.
!> !> !>
!> !> !>
!> !> !>.
!> !> !>
!> !> !
!> !> !
!> !> !
!> !>
!> !
!> !
!> !
!>
!
!
!
Nov 12 '05 #9
Hi Derek,

I have already debugged the sample you listed out. Now it works well. I
think this sample will give you a good understanding of Reflection.

Thanks.

Imports System
Imports System.Reflection

Class test
Shared Sub Main()
Dim obj As forinvoke = New forinvoke
obj.CallMethodsThruReflection()
End Sub
End Class

Class forinvoke
Public Sub CallMethodsThruReflection()
'No Parameters
fnExecuteMethods(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethods(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethods(Me, "fn3", l_objParams3)
'One String Parameter and One Integer Parameter
Dim l_objParams4(1) As Object
l_objParams4(0) = "Hi"
l_objParams4(1) = 20
fnExecuteMethods(Me, "fn4", l_objParams4)
'One String Parameter and One Integer Parameterand One String as
return Parameter
Dim l_objParams5(1) As Object
l_objParams5(0) = "Hi"
l_objParams5(1) = 20
Dim l_strRetVal As String = fnExecuteMethods(Me, "fn5",
l_objParams5).ToString
print("Return Value from fn5 : " & l_strRetVal)
End Sub
Public Function fnExecuteMethods(ByVal p_objObject As Object, ByVal
p_objMethodName As String, Optional ByVal p_objParams() As Object =
Nothing) As Object
' Create a type object and store the type of theobject passed
Dim l_objType As Type = p_objObject.GetType
'Declare a MethodInfo object to store the Methodsof the class
Dim l_objMethodInfo As MethodInfo
'Declare a variable to loop through the Methods ofthis class
Dim l_intMethodCtr As Integer

' Get the MethodInfo for the current class.Binding Flags are
specified to get the
' public and private Methods of this class. WhenPublic or
Non-Public is specified
' in the BindingFlags, it is also necessary tospecify Static or
Instance
l_objMethodInfo = l_objType.GetMethod(p_objMethodName,
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Static Or
BindingFlags.Instance)
Return l_objMethodInfo.Invoke(p_objObject, p_objParams)
End Function

Public Sub fn1()
print("fn1 was called")
End Sub

Public Sub fn2(ByVal p_strData As String)
print("fn2 was called with parameter : " & p_strData)
End Sub

Public Sub fn3(ByVal p_intData As Integer)
print("fn3 was called with parameter : " & p_intData)
End Sub

Public Sub fn4(ByVal p_strData As String, ByVal p_intData As Integer)
print("fn4 was called with parameters : " & p_strData & " AND " &
p_intData)
End Sub

Public Function fn5(ByVal p_strData As String, ByVal p_intData As
Integer) As String
print("fn5 was called with parameters : " & p_strData & " AND " &
p_intData & ". Returning aconcatenated value.")
Return p_strData & p_intData
End Function

Public Sub print(ByVal p_objObject As Object)
Console.WriteLine(p_objObject)
End Sub
End Class

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "Derek Hart" <dm****@gte.net>
!References: <#c*************@tk2msftngp13.phx.gbl>
<#M**************@TK2MSFTNGP11.phx.gbl>
<ee**************@tk2msftngp13.phx.gbl>
<03****************************@phx.gbl>
<OQ**************@TK2MSFTNGP11.phx.gbl>
<nM**************@cpmsftngxa09.phx.gbl>
<eT**************@TK2MSFTNGP10.phx.gbl>
<bm**************@cpmsftngxa09.phx.gbl>
!Subject: Re: Reflection - Run Function From String
!Date: Wed, 9 Jul 2003 08:52:49 -0700
!Lines: 364
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <#I**************@TK2MSFTNGP12.phx.gbl>
!Newsgroups: microsoft.public.dotnet.general
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:100650
!X-Tomcat-NG: microsoft.public.dotnet.general
!
!OK, here is the code slice:
!
!The fnExecuteMethods takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThruReflection()
!
! 'No Parameters
! fnExecuteMethods(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethods(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethods(Me, "fn3", l_objParams3)
! 'One String Parameter and One Integer Parameter
! Dim l_objParams4(1) As Object
! l_objParams4(0) = "Hi"
! l_objParams4(1) = 20
! fnExecuteMethods(Me, "fn4", l_objParams4)
! 'One String Parameter and One Integer Parameter
!and One String as return Parameter
! Dim l_objParams5(1) As Object
! l_objParams5(0) = "Hi"
! l_objParams5(1) = 20
! Dim l_strRetVal As String = fnExecuteMethods
!(Me, "fn5", l_objParams5).ToString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethods(ByVal p_objObject As
!Object, ByVal p_objMethodName As String, Optional ByVal
!p_objParams() As Object = Nothing) As Object
!
! ' Create a type object and store the type of the
!object passed
! Dim l_objType As Type = p_objObject.GetType
! 'Declare a MethodInfo object to store the Methods
!of the class
! Dim l_objMethodInfo As MethodInfo
! 'Declare a variable to loop through the Methods of
!this class
! Dim l_intMethodCtr As Integer
!
! ' Get the MethodInfo for the current class.
!Binding Flags are specified to get the
! ' public and private Methods of this class. When
!Public or Non-Public is specified
! ' in the BindingFlags, it is also necessary to
!specify Static or Instance
! l_objMethodInfo = l_objType.GetMethod
!(p_objMethodName, BindingFlags.NonPublic Or
!BindingFlags.Public Or BindingFlags.Static Or
!BindingFlags.Instance)
!
! Return l_objMethodInfo.Invoke(p_objObject,
!p_objParams)
!
! End Function
!
! Public Sub fn1()
! print("fn1 was called")
! End Sub
!
! Public Sub fn2(ByVal p_strData As String)
! print("fn2 was called with parameter : " &
!p_strData)
! End Sub
!
! Public Sub fn3(ByVal p_intData As Integer)
! print("fn3 was called with parameter : " &
!p_intData)
! End Sub
!
! Public Sub fn4(ByVal p_strData As String, ByVal
!p_intData As Integer)
! print("fn4 was called with parameters : " &
!p_strData & " AND " & p_intData)
! End Sub
!
! Public Function fn5(ByVal p_strData As String, ByVal
!p_intData As Integer) As String
! print("fn5 was called with parameters : " &
!p_strData & " AND " & p_intData & ". Returning a
!concatenated value.")
! Return p_strData & p_intData
! End Function
!
! Public Sub print(ByVal p_objObject As Object)
! Console.WriteLine(p_objObject)
! End Sub
!
!
!
!Derek
!
!
!"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
!news:bm**************@cpmsftngxa09.phx.gbl...
!> Hello Derek,
!>
!> Could you please post the code slice that you used here? The people here
!> should be able to give you some idea. :)
!>
!> Best regards,
!> yhhuang
!> VS.NET, Visual C++
!> Microsoft
!>
!> This posting is provided "AS IS" with no warranties, and confers no
!rights.
!> Got .Net? http://www.gotdotnet.com
!> --------------------
!> !From: "Derek Hart" <dm****@gte.net>
!> !References: <#c*************@tk2msftngp13.phx.gbl>
!> <#M**************@TK2MSFTNGP11.phx.gbl>
!> <ee**************@tk2msftngp13.phx.gbl>
!> <03****************************@phx.gbl>
!> <OQ**************@TK2MSFTNGP11.phx.gbl>
!> <nM**************@cpmsftngxa09.phx.gbl>
!> !Subject: Re: Reflection - Run Function From String
!> !Date: Tue, 8 Jul 2003 20:13:04 -0700
!> !Lines: 216
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !Message-ID: <eT**************@TK2MSFTNGP10.phx.gbl>
!> !Newsgroups: microsoft.public.dotnet.general
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
!> !Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:15009
!> !X-Tomcat-NG: microsoft.public.dotnet.general
!> !
!> !Yes, I have looked at this. I bring in the first routine, and there are
!> !errors in it that I just don't understand. I wish I could find clean
!> !example of this . . .
!> !
!> !Derek
!> !
!> !"Yan-Hong Huang[MSFT]" <yh*****@online.microsoft.com> wrote in message
!> !news:nM**************@cpmsftngxa09.phx.gbl...
!> !> Hello Derek,
!> !>
!> !> Here are many code samples on how to invoke different kinds of methods
!> !> through reflection. In the samples, because the names of the methods
!> being
!> !> invoked are stored in strings, this mechanism provides the capability
!to
!> !> specify methods to be invoked at runtime, rather than at design-time.
!> !>
!> !> http://samples.gotdotnet.com/quickst...oc/Invoke.aspx
!> !>
!> !> Hope it helps.
!> !>
!> !> Best regards,
!> !> yhhuang
!> !> VS.NET, Visual C++
!> !> Microsoft
!> !>
!> !> This posting is provided "AS IS" with no warranties, and confers no
!> !rights.
!> !> Got .Net? http://www.gotdotnet.com
!> !> --------------------
!> !> !From: "Derek Hart" <dm****@gte.net>
!> !> !References: <#c*************@tk2msftngp13.phx.gbl>
!> !> <#M**************@TK2MSFTNGP11.phx.gbl>
!> !> <ee**************@tk2msftngp13.phx.gbl>
!> !> <03****************************@phx.gbl>
!> !> !Subject: Re: Reflection - Run Function From String
!> !> !Date: Tue, 8 Jul 2003 10:22:46 -0700
!> !> !Lines: 163
!> !> !X-Priority: 3
!> !> !X-MSMail-Priority: Normal
!> !> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !> !Message-ID: <OQ**************@TK2MSFTNGP11.phx.gbl>
!> !> !Newsgroups: microsoft.public.dotnet.general
!> !> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> !> 4.60.38.89
!> !> !Path: cpmsftngxa09.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
!> !> !Xref: cpmsftngxa09.phx.gbl microsoft.public.dotnet.general:14973
!> !> !X-Tomcat-NG: microsoft.public.dotnet.general
!> !> !
!> !> !Hi, thank you for responding. When I paste the code in there are a
ton
!> of
!> !> !errors. Should I paste this into a global module? Can you email me
!> this
!> !> in
!> !> !a module or a VB.Net app? Thank you so much for responding. Nobody
!has
!> !> !been able to answer this.
!> !> !
!> !> !
!> !> !"SR" <rs*****@hotmail.com> wrote in message
!> !> !news:03****************************@phx.gbl...
!> !> !Hi
!> !> !
!> !> !Is ur reqmt that "A Function Name will be passed at
!> !> !runtime and you need to execute that function using
!> !> !reflection?".Please confirm...If that is the case here u go
!> !> !
!> !> !The fnExecuteMethods takes an object, a method name and
!> !> !optionally parameters and invokes those methods
!> !> !
!> !> !Public Sub CallMethodsThruReflection()
!> !> !
!> !> ! 'No Parameters
!> !> ! fnExecuteMethods(Me, "fn1")
!> !> ! 'One String Parameter
!> !> ! Dim l_objParams2(0) As Object
!> !> ! l_objParams2(0) = "Hi"
!> !> ! fnExecuteMethods(Me, "fn2", l_objParams2)
!> !> ! 'One Integer Parameter
!> !> ! Dim l_objParams3(0) As Object
!> !> ! l_objParams3(0) = 20
!> !> ! fnExecuteMethods(Me, "fn3", l_objParams3)
!> !> ! 'One String Parameter and One Integer Parameter
!> !> ! Dim l_objParams4(1) As Object
!> !> ! l_objParams4(0) = "Hi"
!> !> ! l_objParams4(1) = 20
!> !> ! fnExecuteMethods(Me, "fn4", l_objParams4)
!> !> ! 'One String Parameter and One Integer Parameter
!> !> !and One String as return Parameter
!> !> ! Dim l_objParams5(1) As Object
!> !> ! l_objParams5(0) = "Hi"
!> !> ! l_objParams5(1) = 20
!> !> ! Dim l_strRetVal As String = fnExecuteMethods
!> !> !(Me, "fn5", l_objParams5).ToString
!> !> ! print("Return Value from fn5 : " & l_strRetVal)
!> !> !
!> !> !
!> !> ! End Sub
!> !> !
!> !> ! Public Function fnExecuteMethods(ByVal p_objObject As
!> !> !Object, ByVal p_objMethodName As String, Optional ByVal
!> !> !p_objParams() As Object = Nothing) As Object
!> !> !
!> !> ! ' Create a type object and store the type of the
!> !> !object passed
!> !> ! Dim l_objType As Type = p_objObject.GetType
!> !> ! 'Declare a MethodInfo object to store the Methods
!> !> !of the class
!> !> ! Dim l_objMethodInfo As MethodInfo
!> !> ! 'Declare a variable to loop through the Methods of
!> !> !this class
!> !> ! Dim l_intMethodCtr As Integer
!> !> !
!> !> ! ' Get the MethodInfo for the current class.
!> !> !Binding Flags are specified to get the
!> !> ! ' public and private Methods of this class. When
!> !> !Public or Non-Public is specified
!> !> ! ' in the BindingFlags, it is also necessary to
!> !> !specify Static or Instance
!> !> ! l_objMethodInfo = l_objType.GetMethod
!> !> !(p_objMethodName, BindingFlags.NonPublic Or
!> !> !BindingFlags.Public Or BindingFlags.Static Or
!> !> !BindingFlags.Instance)
!> !> !
!> !> ! Return l_objMethodInfo.Invoke(p_objObject,
!> !> !p_objParams)
!> !> !
!> !> ! End Function
!> !> !
!> !> ! Public Sub fn1()
!> !> ! print("fn1 was called")
!> !> ! End Sub
!> !> !
!> !> ! Public Sub fn2(ByVal p_strData As String)
!> !> ! print("fn2 was called with parameter : " &
!> !> !p_strData)
!> !> ! End Sub
!> !> !
!> !> ! Public Sub fn3(ByVal p_intData As Integer)
!> !> ! print("fn3 was called with parameter : " &
!> !> !p_intData)
!> !> ! End Sub
!> !> !
!> !> ! Public Sub fn4(ByVal p_strData As String, ByVal
!> !> !p_intData As Integer)
!> !> ! print("fn4 was called with parameters : " &
!> !> !p_strData & " AND " & p_intData)
!> !> ! End Sub
!> !> !
!> !> ! Public Function fn5(ByVal p_strData As String, ByVal
!> !> !p_intData As Integer) As String
!> !> ! print("fn5 was called with parameters : " &
!> !> !p_strData & " AND " & p_intData & ". Returning a
!> !> !concatenated value.")
!> !> ! Return p_strData & p_intData
!> !> ! End Function
!> !> !
!> !> ! Public Sub print(ByVal p_objObject As Object)
!> !> ! Console.WriteLine(p_objObject)
!> !> ! End Sub
!> !> !
!> !> !Hope this helps
!> !> !
!> !> !regards,
!> !> !
!> !> !sr
!> !> !
!> !> !>-----Original Message-----
!> !> !>I have seen this described so that a function from
!> !> !another assembly can be
!> !> !>loaded, but I know the function will be in the compiled
!> !> !assembly that runs
!> !> !>the main program. I am still searching for somebody that
!> !> !has coded this . .
!> !> !>..
!> !> !>
!> !> !>Any ideas?
!> !> !>
!> !> !>Derek
!> !> !>
!> !> !>
!> !> !>"Mattias Sjögren" <ma********************@mvps.org> wrote
!> !> !in message
!> !> !>news:%2****************@TK2MSFTNGP11.phx.gbl...
!> !> !>> Derek,
!> !> !>>
!> !> !>> >I have seen a ton of people discuss how reflection
!> !> !does this, but I
!> !> !>cannot
!> !> !>> >find the syntax to do this. I have tried several code
!> !> !example off of
!> !> !>> >gotdotnet and other articles. Can somebody please
!> !> !show me the code to do
!> !> !>> >this?
!> !> !>>
!> !> !>> There's no really easy way to do this. You'll have to
!> !> !parse out the
!> !> !>> function name and parameters, look up the method you
!> !> !want to call,
!> !> !>> convert the parameter types if needed, and then call
!> !> !the function.
!> !> !>>
!> !> !>>
!> !> !>>
!> !> !>> Mattias
!> !> !>>
!> !> !>> --
!> !> !>> Mattias Sjögren [MVP] mattias @ mvps.org
!> !> !>> http://www.msjogren.net/dotnet/
!> !> !>> Please reply only to the newsgroup.
!> !> !>
!> !> !>
!> !> !>.
!> !> !>
!> !> !
!> !> !
!> !> !
!> !>
!> !
!> !
!> !
!>
!
!
!

Nov 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
2
by: Nancy | last post by:
I have the following situation and could use some advise. I have a Base Class CreditReport, classes BoatCreditReport and HomeCreditReport Inherit from CreditReport. I would like to have the...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Jeff Molby | last post by:
Sorry for the crossposting guys. I figured this one might be a little tricky, so I'm calling upon the C# brains out there to help out this poor VB developer. I know enough C# to translate any code...
0
by: torarvid | last post by:
Hello, I am trying to write a search function that will search through given properties in all objects of a given type for a given phrase, like so: ICollection Search( Type type, string...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
2
by: Jeff | last post by:
I am trying to dynamically load an assembly via reflection and then invoke a method of that assembly that will populate a custom type collection passed into the method byref. I am able to...
0
by: Hans ter Wal via DotNetMonster.com | last post by:
Hi My problem is as follow i'm trying to get a sqlDatareader object back from my shared function by means of reflection. But sadly i'm getting an error: Can anyone steer me in the right...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
2
by: diego | last post by:
hi everyone, i have a sub that opens a form given the form's name as string and opens it using System.Reflection. How can I set the form's properties at runtime. Here is my code. Public Sub...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.