You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Placeholder to fully describe positive and negative test cases.
publicenumABEnum{A=0,B=1}publicclassTest{publicvoidTestAB(){foreach(variteminEnum.GetValues(typeof(ABEnum))){Console.WriteLine(((Enum)item).GetDisplayName());// Feature logged in: https://github.com/DotNetAnalyzers/ReflectionAnalyzers/issues/214// ^^^^^^^^^^^^ bonus: don't raise Possible InvalidCastException if typeof(ABEnum) is an enum defined as an int64 sub-class (default scenario).}}}
Ideally, the code will recommend the correct cast to the appropriate integer size. For example, if the lvalue being assigned to is an int, and the enum is a long, it will recommend a down-cast. If the lvalue being assigned to is a long, and the enum is a short, there will be no loss of precision but it may make sense to warn low severity about data type mismatch.
The text was updated successfully, but these errors were encountered:
I cant seem to reconcile your description with the sample code.
Is the issue with casting the result of Enum.GetValues to the correct type?
foreach(variteminEnum.GetValues(typeof(ABEnum))){Console.WriteLine((ABEnum)item);// ok, we know item is an instance of ABEnumConsole.WriteLine((Enum)item);// ok, ABEnum inherits from EnumConsole.WriteLine((int)(ABEnum)item);// ok, there is a conversion cast from ABEnum to int.Console.WriteLine((int)item);// not ok, we need to perform the unboxing cast before we can perform the conversion cast.}
Placeholder to fully describe positive and negative test cases.
Ideally, the code will recommend the correct cast to the appropriate integer size. For example, if the lvalue being assigned to is an int, and the enum is a long, it will recommend a down-cast. If the lvalue being assigned to is a long, and the enum is a short, there will be no loss of precision but it may make sense to warn low severity about data type mismatch.
The text was updated successfully, but these errors were encountered: