JeffreySu/WeiXinMPSDK · error · InvalidOperationException
无法找到静态方法: .
Error message
无法找到静态方法: {typeName}.{methodName} What it means
After resolving the type, InvokeStaticMethod looks up a public static method by name via type.GetMethod(BindingFlags.Static | BindingFlags.Public). If no such method exists it throws InvalidOperationException naming Type.Method. Only public static parameterless-matching signatures can be found this way.
Solutions
- Verify the method exists and is public static on the target type
- Check the exact method name for the installed SDK version (renames across versions are common)
- If the method is instance-based, obtain an instance first or use an API helper designed for it
- If overloads exist, ensure GetMethod can disambiguate or adjust the invocation approach
Example fix
// before
InvokeWeixinApiHelper.InvokeFullMethod("Senparc.Weixin.MP.CommonApi.GetTokn", args); // typo
// after
InvokeWeixinApiHelper.InvokeFullMethod("Senparc.Weixin.MP.CommonApi.GetToken", args); Defensive patterns
Strategy: try-catch
Validate before calling
var t = Type.GetType(typeName) ?? AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType(typeName, false)).FirstOrDefault(x => x != null);
if (t?.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public) == null) throw new InvalidOperationException($"{typeName}.{methodName} is not a public static method"); Try / catch
try { var result = InvokeWeixinApiHelper.InvokeFullMethod(methodPath, args); }
catch (InvalidOperationException ex) when (ex.Message.Contains("无法找到静态方法")) { log.Error($"Method '{methodPath}' is not public static or was renamed"); } Prevention
- Verify target methods are public static before registering tools
- Regenerate tool definitions after SDK upgrades
- Watch for instance-only APIs that need a wrapper
When it happens
Trigger: The method name portion of fullMethodPath is wrong, the method is instance-level or non-public, or overloads exist that GetMethod cannot match unambiguously given the parameters.
Common situations: Calling an instance API (e.g. an OAuth API requiring an appSecret parameter object) through the static invoker, method renamed in a newer Senparc version, typo in method name from an MCP tool config.
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/58ed6eb6bb164794.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.MCP.Server/InvokeWeixinApiHelper.cs:92
try
{
var type = Type.GetType(typeName);
if (type == null)
{
// 尝试从当前加载的程序集中查找类型
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
type = assembly.GetType(typeName);
if (type != null) break;
}
}
if (type == null)
throw new InvalidOperationException($"无法找到类型: {typeName}");
var method = type.GetMethod(methodName, BindingFlags.Static | BindingFlags.Public);
if (method == null)
throw new InvalidOperationException($"无法找到静态方法: {typeName}.{methodName}");
return method.Invoke(null, parameters);
}
catch (Exception ex)
{
throw new InvalidOperationException($"调用方法 {typeName}.{methodName} 时发生错误", ex);
}
}
/// <summary>
/// 异步调用静态方法
/// </summary>
/// <param name="typeName">类型名称</param>
/// <param name="methodName">方法名称</param>
/// <param name="parameters">参数</param>
/// <returns>方法执行结果</returns>
public async Task<object> InvokeStaticMethodAsync(string typeName, string methodName, object[] parameters)
{View on GitHub (pinned to be573f6f94)