473,839 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 19 '05 #1
9 3702
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.o rg> wrote in message
news:%2******** ********@TK2MSF TNGP11.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
SR
Hi

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

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

Public Sub CallMethodsThru Reflection()

'No Parameters
fnExecuteMethod s(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethod s(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethod s(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
fnExecuteMethod s(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 = fnExecuteMethod s
(Me, "fn5", l_objParams5).T oString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethod s(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.Get Type
'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.GetMe thod
(p_objMethodNam e, BindingFlags.No nPublic Or
BindingFlags.Pu blic Or BindingFlags.St atic Or
BindingFlags.In stance)

Return l_objMethodInfo .Invoke(p_objOb ject,
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.WriteLi ne(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.o rg> wrote in messagenews:%2******* *********@TK2MS FTNGP11.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
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*****@hotmai l.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?".Pl ease confirm...If that is the case here u go

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

Public Sub CallMethodsThru Reflection()

'No Parameters
fnExecuteMethod s(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethod s(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethod s(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
fnExecuteMethod s(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 = fnExecuteMethod s
(Me, "fn5", l_objParams5).T oString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethod s(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.Get Type
'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.GetMe thod
(p_objMethodNam e, BindingFlags.No nPublic Or
BindingFlags.Pu blic Or BindingFlags.St atic Or
BindingFlags.In stance)

Return l_objMethodInfo .Invoke(p_objOb ject,
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.WriteLi ne(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.o rg> wrote in messagenews:%2******* *********@TK2MS FTNGP11.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
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.publi c.dotnet.genera l
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
!Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:14973
!X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!
!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*****@hotmai l.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?".P lease confirm...If that is the case here u go
!
!The fnExecuteMethod s takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThru Reflection()
!
! 'No Parameters
! fnExecuteMethod s(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethod s(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethod s(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
! fnExecuteMethod s(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 = fnExecuteMethod s
!(Me, "fn5", l_objParams5).T oString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethod s(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.Get Type
! '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.GetMe thod
!(p_objMethodNa me, BindingFlags.No nPublic Or
!BindingFlags.P ublic Or BindingFlags.St atic Or
!BindingFlags.I nstance)
!
! Return l_objMethodInfo .Invoke(p_objOb ject,
!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.WriteLi ne(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.o rg> wrote
!in message
!>news:%2****** **********@TK2M SFTNGP11.phx.gb l...
!>> 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
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******** ******@cpmsftng xa09.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.publi c.dotnet.genera l
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
!Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:14973
!X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!
!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*****@hotmai l.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?".P lease confirm...If that is the case here u go
!
!The fnExecuteMethod s takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThru Reflection()
!
! 'No Parameters
! fnExecuteMethod s(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethod s(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethod s(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
! fnExecuteMethod s(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 = fnExecuteMethod s
!(Me, "fn5", l_objParams5).T oString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethod s(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.Get Type
! '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.GetMe thod
!(p_objMethodNa me, BindingFlags.No nPublic Or
!BindingFlags.P ublic Or BindingFlags.St atic Or
!BindingFlags.I nstance)
!
! Return l_objMethodInfo .Invoke(p_objOb ject,
!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.WriteLi ne(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.o rg> wrote
!in message
!>news:%2****** **********@TK2M SFTNGP11.phx.gb l...
!>> 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
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.publi c.dotnet.genera l
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
!Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:15009
!X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!
!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******* *******@cpmsftn gxa09.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.publi c.dotnet.genera l
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
!> !Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:14973
!> !X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!> !
!> !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*****@hotmai l.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?".P lease confirm...If that is the case here u go
!> !
!> !The fnExecuteMethod s takes an object, a method name and
!> !optionally parameters and invokes those methods
!> !
!> !Public Sub CallMethodsThru Reflection()
!> !
!> ! 'No Parameters
!> ! fnExecuteMethod s(Me, "fn1")
!> ! 'One String Parameter
!> ! Dim l_objParams2(0) As Object
!> ! l_objParams2(0) = "Hi"
!> ! fnExecuteMethod s(Me, "fn2", l_objParams2)
!> ! 'One Integer Parameter
!> ! Dim l_objParams3(0) As Object
!> ! l_objParams3(0) = 20
!> ! fnExecuteMethod s(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
!> ! fnExecuteMethod s(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 = fnExecuteMethod s
!> !(Me, "fn5", l_objParams5).T oString
!> ! print("Return Value from fn5 : " & l_strRetVal)
!> !
!> !
!> ! End Sub
!> !
!> ! Public Function fnExecuteMethod s(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.Get Type
!> ! '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.GetMe thod
!> !(p_objMethodNa me, BindingFlags.No nPublic Or
!> !BindingFlags.P ublic Or BindingFlags.St atic Or
!> !BindingFlags.I nstance)
!> !
!> ! Return l_objMethodInfo .Invoke(p_objOb ject,
!> !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.WriteLi ne(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.o rg> wrote
!> !in message
!> !>news:%2****** **********@TK2M SFTNGP11.phx.gb l...
!> !>> 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
OK, here is the code slice:

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

Public Sub CallMethodsThru Reflection()

'No Parameters
fnExecuteMethod s(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethod s(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethod s(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
fnExecuteMethod s(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 = fnExecuteMethod s
(Me, "fn5", l_objParams5).T oString
print("Return Value from fn5 : " & l_strRetVal)
End Sub

Public Function fnExecuteMethod s(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.Get Type
'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.GetMe thod
(p_objMethodNam e, BindingFlags.No nPublic Or
BindingFlags.Pu blic Or BindingFlags.St atic Or
BindingFlags.In stance)

Return l_objMethodInfo .Invoke(p_objOb ject,
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.WriteLi ne(p_objObject)
End Sub

Derek
"Yan-Hong Huang[MSFT]" <yh*****@online .microsoft.com> wrote in message
news:bm******** ******@cpmsftng xa09.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.publi c.dotnet.genera l
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
!Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:15009
!X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!
!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******* *******@cpmsftn gxa09.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.publi c.dotnet.genera l
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
!> !Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:14973
!> !X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!> !
!> !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*****@hotmai l.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?".P lease confirm...If that is the case here u go
!> !
!> !The fnExecuteMethod s takes an object, a method name and
!> !optionally parameters and invokes those methods
!> !
!> !Public Sub CallMethodsThru Reflection()
!> !
!> ! 'No Parameters
!> ! fnExecuteMethod s(Me, "fn1")
!> ! 'One String Parameter
!> ! Dim l_objParams2(0) As Object
!> ! l_objParams2(0) = "Hi"
!> ! fnExecuteMethod s(Me, "fn2", l_objParams2)
!> ! 'One Integer Parameter
!> ! Dim l_objParams3(0) As Object
!> ! l_objParams3(0) = 20
!> ! fnExecuteMethod s(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
!> ! fnExecuteMethod s(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 = fnExecuteMethod s
!> !(Me, "fn5", l_objParams5).T oString
!> ! print("Return Value from fn5 : " & l_strRetVal)
!> !
!> !
!> ! End Sub
!> !
!> ! Public Function fnExecuteMethod s(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.Get Type
!> ! '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.GetMe thod
!> !(p_objMethodNa me, BindingFlags.No nPublic Or
!> !BindingFlags.P ublic Or BindingFlags.St atic Or
!> !BindingFlags.I nstance)
!> !
!> ! Return l_objMethodInfo .Invoke(p_objOb ject,
!> !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.WriteLi ne(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.o rg> wrote
!> !in message
!> !>news:%2****** **********@TK2M SFTNGP11.phx.gb l...
!> !>> 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
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.21 44
@TK2MSFTNGP11.p hx.gbl> <nM************ **@cpmsftngxa09 .phx.gbl> <eTgAGgcRDHA.21 48
@TK2MSFTNGP10.p hx.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.publi c.dotnet.genera l
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net 4.60.38.89
!Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
!Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:100650
!X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!
!OK, here is the code slice:
!
!The fnExecuteMethod s takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThru Reflection()
!
! 'No Parameters
! fnExecuteMethod s(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethod s(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethod s(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
! fnExecuteMethod s(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 = fnExecuteMethod s
!(Me, "fn5", l_objParams5).T oString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethod s(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.Get Type
! '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.GetMe thod
!(p_objMethodNa me, BindingFlags.No nPublic Or
!BindingFlags.P ublic Or BindingFlags.St atic Or
!BindingFlags.I nstance)
!
! Return l_objMethodInfo .Invoke(p_objOb ject,
!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.WriteLi ne(p_objObject)
! End Sub
!
!
!
!Derek
!
!
!"Yan-Hong Huang[MSFT]" <yh*****@online .microsoft.com> wrote in message
!news:bm******* *******@cpmsftn gxa09.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.publi c.dotnet.genera l
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
!> !Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:15009
!> !X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!> !
!> !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******* *******@cpmsftn gxa09.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.publi c.dotnet.genera l
!> !> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> !> 4.60.38.89
!> !> !Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
!> !> !Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:14973
!> !> !X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!> !> !
!> !> !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*****@hotmai l.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?".P lease confirm...If that is the case here u go
!> !> !
!> !> !The fnExecuteMethod s takes an object, a method name and
!> !> !optionally parameters and invokes those methods
!> !> !
!> !> !Public Sub CallMethodsThru Reflection()
!> !> !
!> !> ! 'No Parameters
!> !> ! fnExecuteMethod s(Me, "fn1")
!> !> ! 'One String Parameter
!> !> ! Dim l_objParams2(0) As Object
!> !> ! l_objParams2(0) = "Hi"
!> !> ! fnExecuteMethod s(Me, "fn2", l_objParams2)
!> !> ! 'One Integer Parameter
!> !> ! Dim l_objParams3(0) As Object
!> !> ! l_objParams3(0) = 20
!> !> ! fnExecuteMethod s(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
!> !> ! fnExecuteMethod s(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 = fnExecuteMethod s
!> !> !(Me, "fn5", l_objParams5).T oString
!> !> ! print("Return Value from fn5 : " & l_strRetVal)
!> !> !
!> !> !
!> !> ! End Sub
!> !> !
!> !> ! Public Function fnExecuteMethod s(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.Get Type
!> !> ! '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.GetMe thod
!> !> !(p_objMethodNa me, BindingFlags.No nPublic Or
!> !> !BindingFlags.P ublic Or BindingFlags.St atic Or
!> !> !BindingFlags.I nstance)
!> !> !
!> !> ! Return l_objMethodInfo .Invoke(p_objOb ject,
!> !> !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.WriteLi ne(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.o rg> wrote
!> !> !in message
!> !> !>news:%2****** **********@TK2M SFTNGP11.phx.gb l...
!> !> !>> 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
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.Reflecti on

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

Class forinvoke
Public Sub CallMethodsThru Reflection()
'No Parameters
fnExecuteMethod s(Me, "fn1")
'One String Parameter
Dim l_objParams2(0) As Object
l_objParams2(0) = "Hi"
fnExecuteMethod s(Me, "fn2", l_objParams2)
'One Integer Parameter
Dim l_objParams3(0) As Object
l_objParams3(0) = 20
fnExecuteMethod s(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
fnExecuteMethod s(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 = fnExecuteMethod s(Me, "fn5",
l_objParams5).T oString
print("Return Value from fn5 : " & l_strRetVal)
End Sub
Public Function fnExecuteMethod s(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.Get Type
'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.GetMe thod(p_objMetho dName,
BindingFlags.No nPublic Or BindingFlags.Pu blic Or BindingFlags.St atic Or
BindingFlags.In stance)
Return l_objMethodInfo .Invoke(p_objOb ject, 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.WriteLi ne(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.publi c.dotnet.genera l
!NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
4.60.38.89
!Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
!Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.genera l:100650
!X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!
!OK, here is the code slice:
!
!The fnExecuteMethod s takes an object, a method name and
!optionally parameters and invokes those methods
!
!Public Sub CallMethodsThru Reflection()
!
! 'No Parameters
! fnExecuteMethod s(Me, "fn1")
! 'One String Parameter
! Dim l_objParams2(0) As Object
! l_objParams2(0) = "Hi"
! fnExecuteMethod s(Me, "fn2", l_objParams2)
! 'One Integer Parameter
! Dim l_objParams3(0) As Object
! l_objParams3(0) = 20
! fnExecuteMethod s(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
! fnExecuteMethod s(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 = fnExecuteMethod s
!(Me, "fn5", l_objParams5).T oString
! print("Return Value from fn5 : " & l_strRetVal)
!
!
! End Sub
!
! Public Function fnExecuteMethod s(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.Get Type
! '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.GetMe thod
!(p_objMethodNa me, BindingFlags.No nPublic Or
!BindingFlags.P ublic Or BindingFlags.St atic Or
!BindingFlags.I nstance)
!
! Return l_objMethodInfo .Invoke(p_objOb ject,
!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.WriteLi ne(p_objObject)
! End Sub
!
!
!
!Derek
!
!
!"Yan-Hong Huang[MSFT]" <yh*****@online .microsoft.com> wrote in message
!news:bm******* *******@cpmsftn gxa09.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.publi c.dotnet.genera l
!> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> 4.60.38.89
!> !Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP10.phx.g bl
!> !Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:15009
!> !X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!> !
!> !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******* *******@cpmsftn gxa09.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.publi c.dotnet.genera l
!> !> !NNTP-Posting-Host: lsanca1-ar3-4-60-038-089.lsanca1.dsl-verizon.net
!> !> 4.60.38.89
!> !> !Path: cpmsftngxa09.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
!> !> !Xref: cpmsftngxa09.ph x.gbl microsoft.publi c.dotnet.genera l:14973
!> !> !X-Tomcat-NG: microsoft.publi c.dotnet.genera l
!> !> !
!> !> !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*****@hotmai l.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?".P lease confirm...If that is the case here u go
!> !> !
!> !> !The fnExecuteMethod s takes an object, a method name and
!> !> !optionally parameters and invokes those methods
!> !> !
!> !> !Public Sub CallMethodsThru Reflection()
!> !> !
!> !> ! 'No Parameters
!> !> ! fnExecuteMethod s(Me, "fn1")
!> !> ! 'One String Parameter
!> !> ! Dim l_objParams2(0) As Object
!> !> ! l_objParams2(0) = "Hi"
!> !> ! fnExecuteMethod s(Me, "fn2", l_objParams2)
!> !> ! 'One Integer Parameter
!> !> ! Dim l_objParams3(0) As Object
!> !> ! l_objParams3(0) = 20
!> !> ! fnExecuteMethod s(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
!> !> ! fnExecuteMethod s(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 = fnExecuteMethod s
!> !> !(Me, "fn5", l_objParams5).T oString
!> !> ! print("Return Value from fn5 : " & l_strRetVal)
!> !> !
!> !> !
!> !> ! End Sub
!> !> !
!> !> ! Public Function fnExecuteMethod s(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.Get Type
!> !> ! '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.GetMe thod
!> !> !(p_objMethodNa me, BindingFlags.No nPublic Or
!> !> !BindingFlags.P ublic Or BindingFlags.St atic Or
!> !> !BindingFlags.I nstance)
!> !> !
!> !> ! Return l_objMethodInfo .Invoke(p_objOb ject,
!> !> !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.WriteLi ne(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.o rg> wrote
!> !> !in message
!> !> !>news:%2****** **********@TK2M SFTNGP11.phx.gb l...
!> !> !>> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1586
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 CreditReport class in a separate project from BoatCreditReport and HomecreditReport for departmental code development reasons. I have a CreditFactory class which uses reflection to instantiate the approprite subclass( BoatCreditReport,...
9
1187
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 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?
10
7378
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 Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
1
3230
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 you can offer, if necessary. I have a very loosely coupled WinForms app written in VB.Net, Framework v1.1 I need to take a string value which contains the unqualified name of a class, find the assembly that contains such a class, and...
0
1151
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 PropertiesToSearch, string phrase ) { ... }
6
1922
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 a new thread to display a customer on the screen for example. However, I have a problem with circular references in my objects which means that I have to load the customer object using reflection ie :
2
7778
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 dynamically load both the DALC component (for the method call) and the Entity component (for the custom type collection to pass in), but I keep getting an error ( Message "Object type cannot be converted to target type.") when invoking the method. If I...
0
961
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 direction?
15
2163
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 have an attribute which can be applied to a class definition of a class which inherits from a base, mustinherit class. I want to define methods in the base class which will access the contents of the attribute as it is applied to
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10911
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10589
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10298
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9429
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5683
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.