using reflection to invoke static methods of the class

P

puzzlecracker

Say, I have classname string, static method string, parameters list
(which is char[]). Can I use reflection to invoke the method?
 
S

sternr

Say, I have classname string, static  method string, parameters list
(which is char[]). Can I use reflection to invoke the method?

Yes.
Using Type.GetType("YOUR-TYPE").InvokeMember
The second parameter is BindingFlag, specify BindingFlags.Static for
invocation of static methods.

--sternr
 
A

Arne Vajhøj

puzzlecracker said:
Say, I have classname string, static method string, parameters list
(which is char[]). Can I use reflection to invoke the method?

Yes.

Example:

using System;
using System.Reflection;

namespace E
{
public class Program
{
public static void Test(char[] ca)
{
foreach(char c in ca)
{
Console.Write(c);
}
Console.WriteLine();
}
public static void Main(string[] args)
{
typeof(Program).InvokeMember("Test",
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static |
BindingFlags.InvokeMethod, null, null, new object[] { new char[] { 'H',
'e', 'l', 'l', 'o' }});
Console.ReadKey();
}
}
}

Arne
 
B

Ben Voigt [C++ MVP]

puzzlecracker said:
Say, I have classname string, static method string, parameters list
(which is char[]). Can I use reflection to invoke the method?

Are you referring to a constructor (method name == class name)? Yes you can
use reflection, no most of the code suggested to you isn't going to work
exactly the same for constructor calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top