ArrayTypeMismatchException の原因と直し方
配列の共変性(string[]をobject[]として扱えること)を利用したコードで、実体の型と異なる要素を代入すると発生します。
エラーメッセージの読み方
Unhandled exception. System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
System- 名前空間 — System なら標準の基本例外です
ArrayTypeMismatchException- 例外クラス — 何が起きたか。ここを検索するのが最短です
Attempted to access an element as a type incompatible with the array.- 詳細メッセージ — どの値が問題だったか
at Program.Main(String[] args) in Program.cs:line 5- 発生箇所 — スタックトレースの先頭行
このエラーが出る典型パターン
パターン1
1 class Program { 2 static void Main(string[] args) { 3 string[] strs = new string[3]; 4 object[] objs = strs; 5 objs[0] = 123 ; ^ 6 System.Console.WriteLine(objs[0]); 7 } 8 }
Unhandled exception. System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
at Program.Main(String[] args) in Program.cs:line 5
配列は共変性を持つため、string[]をobject[]として扱えてしまいます。しかし実体は変わらずstring配列なので、string以外を入れると実行時に例外になります。
直し方: 123 を "hi" にします。
広告
広告スロット(未設定)
パターン2
1 class Person { public string Name; } 2 class Program { 3 static void Main(string[] args) { 4 Person[] people = new Person[2]; 5 object[] objs = people; 6 objs[0] = "Al" ; ^ 7 System.Console.WriteLine(objs[0]); 8 } 9 }
Unhandled exception. System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
at Program.Main(String[] args) in Program.cs:line 6
Person型の配列でも同じことが起きます。参照型どうしでも、配列の実体(ここではPerson[])と違う型を入れると実行時に検出されます。
直し方: "Al" を new Person { Name = "Al" } にします。
パターン3
1 class Program { 2 static void Main(string[] args) { 3 string[] names = new string[2]; 4 object[] objs = names; 5 objs[1] = true ; ^ 6 System.Console.WriteLine(objs[1]); 7 } 8 }
Unhandled exception. System.ArrayTypeMismatchException: Attempted to access an element as a type incompatible with the array.
at Program.Main(String[] args) in Program.cs:line 5
boolのような値型を入れても同じ例外になります。配列の実体の型(ここではstring[])だけが正解を決めます。
直し方: true を "bob" にします。
よくある誤解
コンパイラはobject[]への代入を許してしまうため、コンパイル時には気づけません。配列の「見た目の型」と「実体の型」が違うことが原因です。
まとめ
ArrayTypeMismatchExceptionは上級でつまずきやすい項目です。上の3パターンを実際に手で直すと、エラーメッセージのどこを読めばよいかが掴めます。