Uahh/ToastFish · error

导入文件出错!

Error message

导入文件出错!

What it means

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.

Solutions

  1. Rebuild both the app and the CreateLog-containing assembly together so type names match
  2. Verify ImportExcel still returns List<Word>/List<JpWord>/List<CustomizeWord> and the header cell maps to one of those branches
  3. Replace fragile lstObj.ToString() comparison with `lstObj is List<Word> w` pattern matching
  4. Handle null explicitly before the dispatch chain

Example fix

// before
string typeObj = lstObj.ToString();
if (typeObj == typeWord) ...
// after
if (lstObj is List<Word> wl) { Words.WordList = wl; Select.TABLE_NAME = "GRE_2"; }
else if (lstObj is List<JpWord> jl) { Words.JpWordList = jl; Select.TABLE_NAME = "StdJp_Mid"; }
else if (lstObj is List<CustomizeWord> cl) { Words.CustWordList = cl; Select.TABLE_NAME = "自定义"; }
else { MessageBox.Show("导入文件出错!"); return; }
Defensive patterns

Strategy: type-guard

Validate before calling

if (lstObj == null) { MessageBox.Show("导入失败"); return; }
var known = new[]{ typeof(List<Word>), typeof(List<JpWord>), typeof(List<CustomizeWord>) };
if (!known.Contains(lstObj.GetType())) { MessageBox.Show("导入文件出错!"); return; }

Type guard

static bool IsKnownImportType(object o) =>
    o is List<Word> || o is List<JpWord> || o is List<CustomizeWord>;

Try / catch

try { /* dispatch chain */ }
catch (Exception ex) { Debug.WriteLine(ex); MessageBox.Show($"导入文件出错!{ex.Message}"); }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of Uahh/ToastFish@2508784a66 (2026-09-15). Data as JSON: /api/errors/c5b8347132723e7d. Report an issue: GitHub.

Appendix: source

Thrown at View/ToastFish.xaml.cs:441

            {
                if (typeObj == typeWord)
                {
                    Words.WordList = (List<Word>)lstObj;
                    Select.TABLE_NAME = "GRE_2";
                }
                else if (typeObj == typeJpWord)
                {
                    Words.JpWordList = (List<JpWord>)lstObj;
                    Select.TABLE_NAME = "StdJp_Mid";
                }
                else if (typeObj == typeCustWord)
                {
                    Words.CustWordList = (List<CustomizeWord>)lstObj;
                    Select.TABLE_NAME = "自定义";
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("导入文件出错!");
                    return;
                }
            }
            catch {
                System.Windows.Forms.MessageBox.Show("导入文件出错!");
                return;
            }
            
            if (!Directory.Exists("Log")){
                System.IO.Directory.CreateDirectory("Log");
            }
            

            var state = thread.ThreadState;

            if (state == System.Threading.ThreadState.WaitSleepJoin || state == System.Threading.ThreadState.Stopped)
            {
                thread.Abort();

View on GitHub (pinned to 2508784a66)