DotNetAnalyzers / ReflectionAnalyzers

Analyzers checking System.Reflection
MIT License
80 stars 8 forks source link

[BUG] REFL018 on ExpandoObject #216

Open jzabroski opened 5 years ago

jzabroski commented 5 years ago

Library Version Info

Confirmed repro on latest on nuget.org repository:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
    <PackageReference Include="ReflectionAnalyzers" Version="0.1.20-dev">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
</Project>

Bad

dynamic expando = new ExpandoObject();

expando.name = "John"; // fine

Console.WriteLine(expando["name"] // BAD - expando explicitly implements the IDictionary<TKey, TValue> interface

Correct

((IDictionary<String, Object>)expando)["name"]

I believe this is a bug in REFL018. I believe the issue is due to the dynamic keyword silencing errors, but that is just a hunch - I have done zero debugging so far.

JohanLarsson commented 5 years ago

Thanks for reporting!

JohanLarsson commented 5 years ago

The bug is that there is no warning when doing expando["name"]?

jzabroski commented 5 years ago

I need to look. I don't know what REFL018 is. ExpandoObject explicitly implements the IDictionary interface, so my bad vs correct breakdown of usage is correct.

JohanLarsson commented 5 years ago

I'm thinking maybe this is a new ID for checking use of dynamic.

jnm2 commented 4 years ago

Why is explicit dictionary access better?