vinit
vinit

Reputation: 521

method.invoke throwing Value cannot be null exception

I've this code.

MethodInfo method = obj.GetType().GetMethod("Run");
Task task = Task.Factory.StartNew((Action)method.Invoke(obj, null));

I can confirm that obj and method are valid. I can see that the function Run is being invoked as well. But after the method Run completes, I'm getting the below exception :

Message = "Value cannot be null.\r\nParameter name: action"

I'm unable to figure out, which "action" parameter is being referred to here since the function Run doesn't return/accept arguments. Here's the Run method if it helps :

public void Run()
        {
            Console.WriteLine("I'm here");

        }

Upvotes: 0

Views: 616

Answers (1)

Expired Data
Expired Data

Reputation: 678

It's not the parameter to your invoke which cannot be null, it's your parameter to Task.Factory.StartNew.

Just do:

Task.Run(() => method.Invoke(obj, null));

Upvotes: 1

Related Questions