CS.WRONG.CAST.MIGHTThis warning is reported in situations when one object can be cast to another object with the possibility of lost data or even program failure. Vulnerability and riskEither data may be lost, or the program may fail. This can happen when a program tries to access a nonexistent class field after a cast. Example 11 public class Object1 : Object2 { 2 public int a; 3 } 4 public class Object2 { 5 public int b; 6 } 7 public class ClassCastTests { 8 public void foo() { 9 Object1 o1; 10 Object2 o2 = new Object2(); 11 if (flag) 12 o1 = (Object1)o2; 13 } 14 private bool flag; 15 } Object o1 of class Object1 and object o2 of class Object2 are declared on lines 5-6. Then, on line 8, Object2 can be cast to Object1, depending on flag on line 7, which is invalid. |