Reflection - Run Function From String

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

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
 
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
 
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
 
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.


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
 
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/quickstart/howto/doc/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" <[email protected]>
!References: <#[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
!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: <[email protected]>
!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!TK2MSFTNGP11.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.
!
!
!!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
!>
!>
!in message
!>!>> 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.
!>
!>
!>.
!>
!
!
!
 
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
 
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" <[email protected]>
!References: <#[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
!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: <[email protected]>
!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!TK2MSFTNGP10.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
!
!!> 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/quickstart/howto/doc/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" <[email protected]>
!> !References: <#[email protected]>
!> <#[email protected]>
!> <[email protected]>
!> <[email protected]>
!> !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: <[email protected]>
!> !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!TK2MSFTNGP11.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.
!> !
!> !
!> !!> !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
!> !>
!> !>
!> !in message
!> !>!> !>> 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.
!> !>
!> !>
!> !>.
!> !>
!> !
!> !
!> !
!>
!
!
!
 
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
 
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" <[email protected]>
!References: <#[email protected]> <#[email protected]>
<[email protected]> <[email protected]> <OQkLQWXRDHA.2144
@TK2MSFTNGP11.phx.gbl> <[email protected]> <eTgAGgcRDHA.2148
@TK2MSFTNGP10.phx.gbl> <[email protected]>
!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: <#[email protected]>
!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!TK2MSFTNGP12.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
!
!
!!> 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" <[email protected]>
!> !References: <#[email protected]>
!> <#[email protected]>
!> <[email protected]>
!> <[email protected]>
!> <[email protected]>
!> <[email protected]>
!> !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: <[email protected]>
!> !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!TK2MSFTNGP10.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
!> !
!> !!> !> 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/quickstart/howto/doc/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" <[email protected]>
!> !> !References: <#[email protected]>
!> !> <#[email protected]>
!> !> <[email protected]>
!> !> <[email protected]>
!> !> !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: <[email protected]>
!> !> !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!TK2MSFTNGP11.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.
!> !> !
!> !> !
!> !> !!> !> !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
!> !> !>
!> !> !>
!> !> !in message
!> !> !>!> !> !>> 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.
!> !> !>
!> !> !>
!> !> !>.
!> !> !>
!> !> !
!> !> !
!> !> !
!> !>
!> !
!> !
!> !
!>
!
!
!
 
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" <[email protected]>
!References: <#[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
!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: <#[email protected]>
!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!TK2MSFTNGP12.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
!
!
!!> 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" <[email protected]>
!> !References: <#[email protected]>
!> <#[email protected]>
!> <[email protected]>
!> <[email protected]>
!> <[email protected]>
!> <[email protected]>
!> !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: <[email protected]>
!> !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!TK2MSFTNGP10.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
!> !
!> !!> !> 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/quickstart/howto/doc/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" <[email protected]>
!> !> !References: <#[email protected]>
!> !> <#[email protected]>
!> !> <[email protected]>
!> !> <[email protected]>
!> !> !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: <[email protected]>
!> !> !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!TK2MSFTNGP11.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.
!> !> !
!> !> !
!> !> !!> !> !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
!> !> !>
!> !> !>
!> !> !in message
!> !> !>!> !> !>> 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.
!> !> !>
!> !> !>
!> !> !>.
!> !> !>
!> !> !
!> !> !
!> !> !
!> !>
!> !
!> !
!> !
!>
!
!
!
 
Back
Top