The System.MissingMethodException
with the message “Method ‘xyz’ not found” occurs in C# when you attempt to access or invoke a method that does not exist in the specified type. This typically happens when using reflection, dynamic binding, or when there is a mismatch between the method name or signature.
Common Causes and Solutions
- Incorrect Method Name:
If you provide an incorrect method name when using reflection or dynamic binding, this exception will be thrown.
public class Example
{
public void PrintMessage(string message) => Console.WriteLine(message);
}
var method = typeof(Example).GetMethod("PrintMessages"); // Incorrect method name
method.Invoke(new Example(), new object[] { "Hello" }); // MissingMethodException
Fix: Ensure the method name is correct and matches the actual method.
var method = typeof(Example).GetMethod("PrintMessage"); // Correct method name
method.Invoke(new Example(), new object[] { "Hello" }); // Works correctly
- Incorrect Method Signature:
If the method exists but the signature (parameters or return type) does not match, this exception can occur.
public class Example
{
public void PrintMessage(string message) => Console.WriteLine(message);
}
var method = typeof(Example).GetMethod("PrintMessage", new[] { typeof(int) }); // Incorrect parameter type
method.Invoke(new Example(), new object[] { 123 }); // MissingMethodException
Fix: Ensure the method signature matches exactly.
var method = typeof(Example).GetMethod("PrintMessage", new[] { typeof(string) }); // Correct parameter type
method.Invoke(new Example(), new object[] { "Hello" }); // Works correctly
- Overloaded Methods:
If there are multiple overloaded methods and you do not specify the correct one, this exception can occur.
public class Example
{
public void PrintMessage(string message) => Console.WriteLine(message);
public void PrintMessage(string message, int count) => Console.WriteLine($"{message} {count}");
}
var method = typeof(Example).GetMethod("PrintMessage"); // Ambiguous method
method.Invoke(new Example(), new object[] { "Hello" }); // MissingMethodException
Fix: Specify the exact method by providing parameter types.
var method = typeof(Example).GetMethod("PrintMessage", new[] { typeof(string) }); // Correct method
method.Invoke(new Example(), new object[] { "Hello" }); // Works correctly
- Using
dynamic
:
When usingdynamic
, if the method does not exist on the object, this exception can occur at runtime.
dynamic obj = new Example();
obj.PrintMessages("Hello"); // MissingMethodException – Method does not exist
Fix: Ensure the method exists on the object.
obj.PrintMessage("Hello"); // Correct method name
- Version Mismatch or Missing Assembly:
If the method exists in a different version of the assembly or the assembly is missing, this exception can occur. Fix: Ensure the correct version of the assembly is referenced and the method exists. - Private or Non-Public Methods:
If the method is private or non-public and you attempt to access it without proper reflection flags, this exception can occur.
public class Example
{
private void PrintMessage(string message) => Console.WriteLine(message);
}
var method = typeof(Example).GetMethod("PrintMessage"); // MissingMethodException – Private method
Fix: Use BindingFlags
to access non-public methods.
var method = typeof(Example).GetMethod("PrintMessage", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(new Example(), new object[] { "Hello" }); // Works correctly
- Incorrect Use of Generics:
If you attempt to invoke a generic method without specifying the correct type arguments, this exception can occur.
public class Example
{
public void PrintMessage<T>(T message) => Console.WriteLine(message);
}
var method = typeof(Example).GetMethod("PrintMessage"); // MissingMethodException – Generic method
Fix: Specify the type arguments for the generic method.
var method = typeof(Example).GetMethod("PrintMessage").MakeGenericMethod(typeof(string));
method.Invoke(new Example(), new object[] { "Hello" }); // Works correctly