{"record":{"id":"a6c6ada49afe82cd","repo":"JeffreySu/WeiXinMPSDK","slug":"platformtype-nameof-platformtype-platformtype-tostring","errorCode":null,"errorMessage":"未知的 PlatformType {nameof(platformType)}：{platformType.ToString()}","messagePattern":"未知的 PlatformType (.+?)：(.+?)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainer.cs","lineNumber":354,"sourceCode":"        {\n            string appId = null;\n            switch (platformType)\n            {\n                case PlatformType.MP:\n                    appId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;\n                    break;\n                case PlatformType.Open:\n                    appId = Senparc.Weixin.Config.SenparcWeixinSetting.WeixinAppId;\n                    break;\n                case PlatformType.WxOpen:\n                    appId = Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenAppId;\n                    break;\n                case PlatformType.QY:\n                    break;\n                case PlatformType.Work:\n                    break;\n                default:\n                    throw new ArgumentOutOfRangeException($\"未知的 PlatformType {nameof(platformType)}：{platformType.ToString()}\");\n            }\n\n            if (appId == null)\n            {\n                var firstBag = GetAllItems().FirstOrDefault() as IBaseContainerBag_AppId;\n                appId = firstBag == null ? null : firstBag.AppId;\n            }\n\n            return appId;\n        }\n\n        ///// <summary>\n        ///// 获取完整的数据集合的列表，包括所有的Container数据在内（建议不要进行任何修改操作）\n        ///// </summary>\n        ///// <returns></returns>\n        //public static IDictionary<string, IContainerItemCollection> GetCollectionList()\n        //{\n        //    return CollectionList;","sourceCodeStart":336,"sourceCodeEnd":372,"githubUrl":"https://github.com/JeffreySu/WeiXinMPSDK/blob/be573f6f94bdbf718dd5f6cdecb137fbc7ff651e/src/Senparc.Weixin/Senparc.Weixin/Containers/BaseContainer.cs#L336-L372","documentation":"GetFirstOrDefaultAppId switches on the PlatformType parameter to apply platform-specific filtering. QY/Work (and some others) are no-ops, and any other/unexpected PlatformType value reaches default and throws ArgumentOutOfRangeException naming the type and value.","triggerScenarios":"Calling GetFirstOrDefaultAppId (directly or via container helpers that accept platformType) with a PlatformType value not covered by the switch — typically an undefined enum cast or a newly added PlatformType the installed library version doesn't handle.","commonSituations":"Persisting PlatformType as an int in a database/config and casting it back without validation; upgrading WeChat platform types (e.g. Enterprise WeChat -> WeCom) with an older library version that lacks the case; passing default(PlatformType) when 0 isn't a handled member.","solutions":["Pass only PlatformType values supported by your library version (validate with Enum.IsDefined and check the version's handled set).","Upgrade Senparc.Weixin if you're using a newer platform type (e.g. WeCom) added after your version.","Normalize stored platform values: map legacy/unknown ints to a known member before calling.","Wrap the call and fall back to enumerating bags yourself via GetAllItems() if platform filtering isn't required."],"exampleFix":"// before\nvar appId = container.GetFirstOrDefaultAppId((PlatformType)record.PlatformInt);\n// after\nif (!Enum.IsDefined(typeof(PlatformType), record.PlatformInt))\n{\n    record.PlatformInt = (int)PlatformType.MP; // or reject\n}\nvar appId = container.GetFirstOrDefaultAppId((PlatformType)record.PlatformInt);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(PlatformType), platformType))\n    throw new ArgumentException($\"Unknown PlatformType: {platformType}\");","typeGuard":"bool IsKnownPlatform(PlatformType p) => Enum.IsDefined(typeof(PlatformType), p);","tryCatchPattern":"try\n{\n    var appId = container.GetFirstOrDefaultAppId(platformType);\n}\ncatch (ArgumentOutOfRangeException ex)\n{\n    Log($\"Unhandled PlatformType: {ex.Message}; falling back.\");\n    var appId = GetAllItems().OfType<IBaseContainerBag_AppId>().FirstOrDefault()?.AppId;\n}","preventionTips":["Persist PlatformType with its enum name, not raw ints.","Validate stored platform values at load time and map legacy values.","Upgrade the library when adopting new WeChat platform types.","Centralize platform handling and keep switch coverage tests."],"tags":["platform","enum","container","argument-out-of-range"],"backgroundTag":"invalid-enum-value","analyzedSha":"be573f6f94bdbf718dd5f6cdecb137fbc7ff651e","analyzedAt":"2026-09-12T10:01:50.733Z","contentChangedAt":"2026-09-12T10:01:50.733Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}