{"record":{"id":"c5b8347132723e7d","repo":"Uahh/ToastFish","slug":"toastfish-xaml","errorCode":null,"errorMessage":"导入文件出错！","messagePattern":"导入文件出错！","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"View/ToastFish.xaml.cs","lineNumber":441,"sourceCode":"            {\n                if (typeObj == typeWord)\n                {\n                    Words.WordList = (List<Word>)lstObj;\n                    Select.TABLE_NAME = \"GRE_2\";\n                }\n                else if (typeObj == typeJpWord)\n                {\n                    Words.JpWordList = (List<JpWord>)lstObj;\n                    Select.TABLE_NAME = \"StdJp_Mid\";\n                }\n                else if (typeObj == typeCustWord)\n                {\n                    Words.CustWordList = (List<CustomizeWord>)lstObj;\n                    Select.TABLE_NAME = \"自定义\";\n                }\n                else\n                {\n                    System.Windows.Forms.MessageBox.Show(\"导入文件出错！\");\n                    return;\n                }\n            }\n            catch {\n                System.Windows.Forms.MessageBox.Show(\"导入文件出错！\");\n                return;\n            }\n            \n            if (!Directory.Exists(\"Log\")){\n                System.IO.Directory.CreateDirectory(\"Log\");\n            }\n            \n\n            var state = thread.ThreadState;\n\n            if (state == System.Threading.ThreadState.WaitSleepJoin || state == System.Threading.ThreadState.Stopped)\n            {\n                thread.Abort();","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/Uahh/ToastFish/blob/2508784a6650862a60e1f3ec3a54dd83efdec98b/View/ToastFish.xaml.cs#L423-L459","documentation":"In ImportWords_Click, the object returned by ImportExcel is matched by type name against List<Word>, List<JpWord>, and List<CustomizeWord>. If the runtime type matches none of them (ImportExcel returned an unexpected type), the else branch shows '导入文件出错！' and aborts the import.","triggerScenarios":"ImportExcel returns something other than the three known list types — practically only possible if the ImportExcel code changed or a different CreateLog implementation is used; also reached if lstObj is null (lstObj.ToString() would have thrown earlier, routing to the catch instead).","commonSituations":"Code drift: a developer added a new return type in ImportExcel but not in the dispatch chain; type name comparison via ToString() breaking after renaming/refactoring namespaces; loading a stale DLL where the two assemblies disagree.","solutions":["Rebuild both the app and the CreateLog-containing assembly together so type names match","Verify ImportExcel still returns List<Word>/List<JpWord>/List<CustomizeWord> and the header cell maps to one of those branches","Replace fragile lstObj.ToString() comparison with `lstObj is List<Word> w` pattern matching","Handle null explicitly before the dispatch chain"],"exampleFix":"// before\nstring typeObj = lstObj.ToString();\nif (typeObj == typeWord) ...\n// after\nif (lstObj is List<Word> wl) { Words.WordList = wl; Select.TABLE_NAME = \"GRE_2\"; }\nelse if (lstObj is List<JpWord> jl) { Words.JpWordList = jl; Select.TABLE_NAME = \"StdJp_Mid\"; }\nelse if (lstObj is List<CustomizeWord> cl) { Words.CustWordList = cl; Select.TABLE_NAME = \"自定义\"; }\nelse { MessageBox.Show(\"导入文件出错！\"); return; }","handlingStrategy":"type-guard","validationCode":"if (lstObj == null) { MessageBox.Show(\"导入失败\"); return; }\nvar known = new[]{ typeof(List<Word>), typeof(List<JpWord>), typeof(List<CustomizeWord>) };\nif (!known.Contains(lstObj.GetType())) { MessageBox.Show(\"导入文件出错！\"); return; }","typeGuard":"static bool IsKnownImportType(object o) =>\n    o is List<Word> || o is List<JpWord> || o is List<CustomizeWord>;","tryCatchPattern":"try { /* dispatch chain */ }\ncatch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show($\"导入文件出错！{ex.Message}\"); }","preventionTips":["Use `is` pattern matching instead of type-name string comparison","Keep ImportExcel return types and the dispatch chain in sync (add new types in both places)","Rebuild all assemblies together after refactoring type names","Null-check the import result before dispatch"],"tags":["csharp","import","type-mismatch","winforms"],"backgroundTag":"type-mismatch","analyzedSha":"2508784a6650862a60e1f3ec3a54dd83efdec98b","analyzedAt":"2026-09-15T23:21:32.897Z","contentChangedAt":"2026-09-15T23:21:32.897Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}