JeffreySu/WeiXinMPSDK · error · InvalidOperationException
无法找到类型
Error message
无法找到类型: {typeName} What it means
InvokeStaticMethod resolves the type portion of a full method path by searching loaded assemblies via Assembly.GetType. When no assembly exposes a type matching typeName it throws InvalidOperationException with the type name. This happens before any method lookup, so the failure is purely type resolution.
Solutions
- Use the fully-qualified type name including namespace (e.g. Senparc.Weixin.MP.CommonApi)
- Ensure the assembly containing the type is referenced and loaded before invoking (or register it so Assembly.Load resolves it)
- Check the exact class name against the installed Senparc SDK version (names change between versions)
- For nested/generic types, pass the runtime-correct name (e.g. Namespace.Outer+Inner)
Example fix
// before
InvokeWeixinApiHelper.InvokeFullMethod("CommonApi.GetToken", args);
// after
InvokeWeixinApiHelper.InvokeFullMethod("Senparc.Weixin.MP.CommonApi.GetToken", args); Defensive patterns
Strategy: try-catch
Validate before calling
var type = AppDomain.CurrentDomain.GetAssemblies().Select(a => a.GetType(typeName, false)).FirstOrDefault(t => t != null);
if (type == null) throw new InvalidOperationException($"Type '{typeName}' not loaded — check spelling, namespace and project references"); Try / catch
try { var result = InvokeWeixinApiHelper.InvokeFullMethod(methodPath, args); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("无法找到类型")) { log.Error($"Unknown type in '{methodPath}': check SDK version and references"); } Prevention
- Always use fully-qualified type names
- Pin and document the Senparc SDK version the tool paths were generated for
- Reference every package whose types the MCP server exposes
When it happens
Trigger: Calling InvokeFullMethod with a type name that is misspelled, not namespace-qualified, lives in an assembly that is not loaded into the AppDomain, or differs across library versions.
Common situations: Renamed classes after a Senparc SDK version upgrade, forgetting a namespace prefix, targeting a type in a NuGet package not referenced/loaded by the MCP server host, generic or nested types needing assembly-qualified names.
Related errors
AI-assisted analysis of JeffreySu/WeiXinMPSDK@be573f6f94 (2026-09-12).
Data as JSON: /api/errors/e36af3eaa3f2c660.
Report an issue: GitHub.
Appendix: source
Thrown at src/Senparc.Weixin.MCP.Server/InvokeWeixinApiHelper.cs:88
/// <param name="parameters">参数</param>
/// <returns>方法执行结果</returns>
public object InvokeStaticMethod(string typeName, string methodName, object[] parameters)
{
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>View on GitHub (pinned to be573f6f94)