469,328 Members | 1,301 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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(myNamespace + "." + 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.CreateInstance(myType);

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 22282
If you have a type object for your static class you can do the
following.

Type myStaticClassType;
//...
Object[] myStaticMethodArguments;
//...

MethodInfo mystaticMethodInfo =
myStaticClassType.GetMethod(myStaticMethodName);
Object myStaticMethodResult = mystaticMethodInfo.Invoke(null,
myStaticMethodArguments);

// do something usefull with myStaticMethodResult

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*******@discussions.microsoft.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(myNamespace + "." + 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.CreateInstance(myType);

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*******@discussions.microsoft.comwrote in message
news:D5**********************************@microsof t.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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by trialproduct2004 | last post: by
6 posts views Thread by ketan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.