C# 7 pattern matching for dynamic types
Consider the following code:
Microsoft.Office.Interop.Excel.Range myRange = this.Cells[1,1];
var myString = myRange.Value2 as string;
if (myString != null)
{
...
}
R# prompts me to 'Use pattern matching' which results in
if (myRange.Value2 is string myString)
{
...
}
while this gets compiled without a problem, when running MSbuild it throws a
error CS8121: An expression of type dynamic cannot be handled by a pattern of type string
From this post https://stackoverflow.com/a/41495614/839780 it looks like it's not supported properly.
I have to manually disable UsePatternMatching whenever it suggests it on dynamic types which is quite annoying.
Can this be fixed?
Thanks
P.S
This also applies to MergeCastWithTypeCheck for dynamic types
Please sign in to leave a comment.
The StackOverflow issue you've linked to should be fixed in C# 7.1: https://github.com/dotnet/roslyn/issues/16195
But as far as I can see, that issue refers to generic types, not dynamic types. I don't know whether the fix for generic types also fixed dynamic types at the same time.