Reputation: 371
I'm using a public static class and static method with its parameters:
public static class WLR3Logon
{
static void getLogon(int accountTypeID)
{}
}
Now I am trying to fetch the method with its parameters into another class and using the following code:
MethodInfo inf = typeof(WLR3Logon).GetMethod("getLogon",
BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);
int[] parameters = { accountTypeId };
foreach (int parameter in parameters)
{
inf.Invoke("getLogon", parameters);
}
But its giving me error
"Object reference not set to an instance of an object."
Where I'm going wrong.
Upvotes: 7
Views: 37700
Reputation: 371
This problem got solved by using the following approach:
using System.Reflection;
string methodName = "getLogon";
Type type = typeof(WLR3Logon);
MethodInfo info = type.GetMethod(
methodName,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
object value = info.Invoke(null, new object[] { accountTypeId } );
Upvotes: 19
Reputation: 20731
Your method is private as you have not explicitly declared an access modifier. You have two options to make your code work as intended:
public
.BindingFlags.NonPublic
in the GetMethod
callUpvotes: 3
Reputation: 15130
There are many problems here
Upvotes: 7
Reputation: 19252
make your method public
. It should work after that
public static class WLR3Logon
{
public static void getLogon(int accountTypeID)
{}
}
Upvotes: 1