473,692 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoking a Static Method in a Static Class

I want to invoke a static method of a static class using a TEXT variable
which has the namespace and class name which should be executed. I looked at
the "CreateInstance " method, but this looks like a way to create an instance
of a class, which is not what is needed. I attempted to use this method,
even though I'm pretty sure that it is not applicable here and it fails as
the "Type" variable is null after I use the following instruction:

string myNamespace = "Fred.Utilities ";
string clsName = "MyClass.cs ";
Type myType = Type.GetType(my Namespace + "." + clsName);

To create an instance of the object (assuming a no parameter constructor -
And again, Since this is a static class with no constructor, I know that it
is not applicable, but I do intend to use the same sort of set up to create a
class which is not a static class and I was wanting to confirm that this
would be appropriate in that instance).

object obj = Activator.Creat eInstance(myTyp e);

Would this be the appropriate set of instructions to use on an Instance class?
How would I provide the arguements to the constructor if they were required?
How should I go about this for a static class/method?

Thanks in advance for your assistance!

After those instructions are created, myType = null.
Type
Feb 7 '07 #1
3 22574
If you have a type object for your static class you can do the
following.

Type myStaticClassTy pe;
//...
Object[] myStaticMethodA rguments;
//...

MethodInfo mystaticMethodI nfo =
myStaticClassTy pe.GetMethod(my StaticMethodNam e);
Object myStaticMethodR esult = mystaticMethodI nfo.Invoke(null ,
myStaticMethodA rguments);

// do something usefull with myStaticMethodR esult

Feb 7 '07 #2

For a static method you don't need to create an instance at all. Use
reflection to get the MethodInfo for the target method and then call
Invoke, passing null as the object reference.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Wed, 7 Feb 2007 06:53:01 -0800, JimHeavey
<Ji*******@disc ussions.microso ft.comwrote:
>I want to invoke a static method of a static class using a TEXT variable
which has the namespace and class name which should be executed. I looked at
the "CreateInstance " method, but this looks like a way to create an instance
of a class, which is not what is needed. I attempted to use this method,
even though I'm pretty sure that it is not applicable here and it fails as
the "Type" variable is null after I use the following instruction:

string myNamespace = "Fred.Utilities ";
string clsName = "MyClass.cs ";
Type myType = Type.GetType(my Namespace + "." + clsName);

To create an instance of the object (assuming a no parameter constructor -
And again, Since this is a static class with no constructor, I know that it
is not applicable, but I do intend to use the same sort of set up to create a
class which is not a static class and I was wanting to confirm that this
would be appropriate in that instance).

object obj = Activator.Creat eInstance(myTyp e);

Would this be the appropriate set of instructions to use on an Instance class?
How would I provide the arguements to the constructor if they were required?
How should I go about this for a static class/method?

Thanks in advance for your assistance!

After those instructions are created, myType = null.
Type
Feb 7 '07 #3
Hi,

"JimHeavey" <Ji*******@disc ussions.microso ft.comwrote in message
news:D5******** *************** ***********@mic rosoft.com...
|I want to invoke a static method of a static class using a TEXT variable
| which has the namespace and class name which should be executed. I looked
at
| the "CreateInstance " method, but this looks like a way to create an
instance
| of a class, which is not what is needed. I attempted to use this method,
| even though I'm pretty sure that it is not applicable here and it fails as
| the "Type" variable is null after I use the following instruction:

If the method is static you dont have to create an instance, if you get a
Type instance of your class you can do InvokeMethod passing null for the
instance parameter.

--
Ignacio Machin
machin AT laceupsolutions com
Feb 7 '07 #4

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

Similar topics

3
4933
by: trialproduct2004 | last post by:
hi all i am having application which is using two classes. in one class i am creating instance of another class and calling method of that class. what i want is from inner class's method i want to invoke method of outer class. can i use delegates to do this. any help will be appreciated. thanks in advance.
22
2758
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
8
1557
by: Larry Lard | last post by:
Today I discovered that a syntax that I thought was forbidden in C# but allowed in VB.NET (and I _don't like_ that it's allowed in VB.NET) is actually allowed in C#. Which confused me. The syntax in question is referring to static members of a class *from a subclass*. (Now, I know for sure there's a similar difference, in that VB.NET allows/ed you to invoke static members from an instance member, whereas C# has always required you to...
12
3433
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this static constructor of the base class would be invoked even if a an object of the derived class is created. Is this correct ? Is there any way to ensure the base class's static constructor is invoked before the derived class instance is constructed ?...
6
10537
by: ketan | last post by:
Hi All, I have a situation where in I need to call static methods of the derived class. I checked previous posts, but could not get any satisfactory reply. My situation can be simulated with following code. // // Does not compile. Not correct C++ syntax - shows what needs to be
1
2782
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList, List<VtheSecondList); I pass the name of the method, the arguments for the "GenericMethodWithGenericArguments" to another method, which is supposed to invoke this method using the Invoke method in the MethodInfo class. My process of invocation is as follows
2
1613
by: gwoodhouse | last post by:
Hello all, I have an application at the moment which uses forms. The main form (which has ALOT of things going on it in) also happens to do alot of network proccess's which ive put in Threads. DUring refactoring i decided i wanted to seperate as much non-form related logical from the main page as i could. I created a new static class, thinking i could call a "ThreadStart" method which, when it needs to access form elements, drops into...
6
2738
by: DaTurk | last post by:
I know in a WinForm you can check if an Invoke is inquired and invoke a method on the main thread. But can you do this in a class? Can I call Invoke(someMethod) in my asynch event handler? And if I can't do it this way, is there a way I can do it? Thanks.
0
916
by: Dobedani | last post by:
Dear All, For some time now, I have been working with the ctypes module on Windows. First I got my hands on a library developed with Delphi 7. That library is exporting plain functions. Internally, reference is made to the instance of a class, which is instantiated in a kind of initialization section. Everything works fine. Now, I have developed a library in C#. I am able to get the result from static methods, e.g. public static String...
0
8610
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
8544
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
8810
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
7633
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...
1
6462
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5821
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4325
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...
1
2977
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2242
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.