By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,860 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

Reflection - Run Function From String

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.

Jul 19 '05 #2

P: n/a
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.

.

Jul 19 '05 #3

P: n/a
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.

.

Jul 19 '05 #4

P: n/a
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.
!>
!>
!>.
!>
!
!
!

Jul 19 '05 #5

P: n/a
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.
!>
!>
!>.
!>
!
!
!

Jul 19 '05 #6

P: n/a
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.
!> !>
!> !>
!> !>.
!> !>
!> !
!> !
!> !
!>
!
!
!

Jul 19 '05 #7

P: n/a
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.
!> !>
!> !>
!> !>.
!> !>
!> !
!> !
!> !
!>
!
!
!

Jul 19 '05 #8

P: n/a
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.
!> !> !>
!> !> !>
!> !> !>.
!> !> !>
!> !> !
!> !> !
!> !> !
!> !>
!> !
!> !
!> !
!>
!
!
!
Jul 19 '05 #9

P: n/a
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.
!> !> !>
!> !> !>
!> !> !>.
!> !> !>
!> !> !
!> !> !
!> !> !
!> !>
!> !
!> !
!> !
!>
!
!
!

Jul 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.