Skip to content

Commit 8329821

Browse files
committed
#3518: Add tests that try unboxing to the wrong type in a number of different ways.
1 parent 5fb2385 commit 8329821

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

ICSharpCode.Decompiler.Tests/TestCases/Correctness/TrickyTypes.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static void Main()
3030
StringConcat();
3131
LinqNullableMin();
3232
LinqNullableMin(1, 2, 3);
33+
UnboxingToWrongType();
3334
}
3435

3536
static void Print<T>(T val)
@@ -110,5 +111,49 @@ static void LinqNullableMin(params int[] arr)
110111
Print(string.Format("LinqNullableMin {0}:", arr.Length));
111112
Print(arr.Min(v => (int?)v));
112113
}
114+
115+
static void UnboxingToWrongType()
116+
{
117+
Console.WriteLine("UnboxingToWrongType:");
118+
object o = 3.50;
119+
try
120+
{
121+
Print((long)o);
122+
}
123+
catch (Exception ex)
124+
{
125+
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
126+
}
127+
try
128+
{
129+
Print(o as long?);
130+
}
131+
catch (Exception ex)
132+
{
133+
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
134+
}
135+
try
136+
{
137+
Print((long)(o as long?));
138+
}
139+
catch (Exception ex)
140+
{
141+
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
142+
}
143+
try
144+
{
145+
Print((long)(o is long ? o : null));
146+
}
147+
catch (Exception ex)
148+
{
149+
Console.WriteLine(ex.GetType().Name + ": " + ex.Message);
150+
}
151+
#if CS90
152+
if (o is not 0L)
153+
{
154+
Console.WriteLine("Not 0L");
155+
}
156+
#endif
157+
}
113158
}
114159
}

0 commit comments

Comments
 (0)