473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to call shared methods by string name?

Don
Is it possible to call a shared method of a class when you have both the
name of the class and the name of the method of that class that you want to
invoke stored in strings? If so, can this be done for singleton classes
(i.e. classes with only shared members and with a private instance
constructor)?

- Don
Nov 21 '05 #1
2 3855
"Don" <un*****@oblivion.com> schrieb
Is it possible to call a shared method of a class when you have both
the name of the class and the name of the method of that class that
you want to invoke stored in strings? If so, can this be done for
singleton classes (i.e. classes with only shared members and with a
private instance constructor)?

Class Form1
Shared Sub test()

End Sub

Sub AnyWhere
Type.GetType("Rootnamespace.Form1").GetMethod("tes t").Invoke( _
Nothing, Nothing _
)
End Sub

End Class

Armin

Nov 21 '05 #2
Don
"Armin Zingler" <az*******@freenet.de> wrote in message
news:uV**************@TK2MSFTNGP10.phx.gbl...
"Don" <un*****@oblivion.com> schrieb
Is it possible to call a shared method of a class when you have both
the name of the class and the name of the method of that class that
you want to invoke stored in strings? If so, can this be done for
singleton classes (i.e. classes with only shared members and with a
private instance constructor)?

Type.GetType("Rootnamespace.Form1").GetMethod("tes t").Invoke( _
Nothing, Nothing _
)


I was a little thrown off by the inclusion of the Rootnamespace in the type
name, thinking I could use it to reference namespaces of referenced
libraries. That didn't work. However, the following did work:
' Get the referenced assembly that contains the class
Dim asm As System.Reflection.Assembly = _
System.Reflection.Assembly.LoadWithPartialName("As semblyName")

' Get the class's type
Dim typ As Type = asm.GetType("AssemblyName.ClassType", True, True)

' Get the method we want to execute
Dim mi As System.Reflection.MethodInfo = typ.GetMethod("MethodName")

' Execute method
mi.Invoke(Nothing, Nothing)

' Alternatively: Execute method with arguments
'mi.Invoke(Nothing, New Object() {arg1, arg2, arg3})
Or, as one line:

System.Reflection.Assembly.LoadWithPartialName("As semblyName").GetType("Asse
mblyName.ClassType", True, True).GetMethod("MethodName").Invoke(Nothing,
Nothing)
- Don
Nov 21 '05 #3

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

Similar topics

6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
5
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the...
26
by: Adam Warner | last post by:
Hello all, I'm very new to C but I have a number of years of Common Lisp programming experience. I'm trying to figure out ways of translating higher order concepts such as closures into C. The...
11
by: tshad | last post by:
I am setting up some of my functions in a class called MyFunctions. I am not clear as to the best time to set a function as Shared and when not to. For example, I have the following bit...
14
by: Joe Fallon | last post by:
I am trying to build a Data Access Layer for a SQL Server back end. My class has a number of methods in it. The constructor takes a connection string. I currently have to instantiate an object...
33
by: Joe Fallon | last post by:
1. I have a Base class that has a certain amount of functionality. 2. Then I have a CodeSmith generated class that inherits from the Base class and adds functionality. 3. Since I want to be able...
5
by: Erik Cruz | last post by:
Hello! I have read some threads discussing the fact that a module is in reality a shared class. If I try to create a Public Shared Class in vb.net I receive a compile error. Why? If I can't...
2
by: mgoold2002 | last post by:
Hello. I've just begun programming in VB .NET, and I'm trying to turn all my modules into classes. In order to retrieve/exchange values from one class to another, I initiated New instances of the...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.