Nischal Chitta [mailto:[EMAIL PROTECTED]] wrote:

> I have a class A, which has a static method M1(). I want to
> invoke this method at runtime without instantiating the class
> using Reflection. How can I do it??

I have a feeling what your asking may not be exactly what you're looking for
because I don't quite understand why you think you need to instantiate an
instance of the class being reflected to execute a static method. However,
if this really is all you're looking for, here's how you'd do it.

<codeSnippet language="C#">
// Get a Type instance of the class you want to reflect on
// (note: there's a bunch of ways to do this, but I'm gonna go
// with the static approach here for simplicity)
Type typeToReflect = typeof(MyClass);

// You may need to use an overloaded version of GetMethod depending
// on your needs (i.e. whether or not there are parameters)
// Also a diff. set of BindingFlags may better suit your needs
MethodInfo myStaticMethodInfo = typeToReflect.GetMethod("MyStaticMethod",
BindingFlags.Static|BindingFlags.Public);

// Assuming there's no params
myStaticMethodInfo.Invoke(null, null);
</codeSnippet>

HTH,
Drew

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to