Skip to content

REFL003 incorrectly assumes that .GetType() returns only the type seen at compile-time #183

Open
@jnm2

Description

@jnm2

0.1.16-dev

void M(IEnumerable<object> objectImplementingIEnumerable)
{
    objectImplementingIEnumerable.GetType().GetMethod("Add");
}

REFL003 The type System.Collections.Generic.IEnumerable<object> does not have a member named Add.

The analyzer should bail if it sees a GetType() on an expression of a virtual type since it can know nothing about the expected concrete type which may be internal to an external lib.

Additionally, if it sees a GetType() on a nonvirtual type, maybe it should bail anyway because it will be reporting #169 in all these cases anyway.


Actual code, in case you wonder why you would do this:

        public static IReadOnlyCollection<Assembly> GetAssemblies(this ITypeDiscoveryService typeDiscoveryService)
        {
            var serviceType = typeDiscoveryService.GetType();
            if (serviceType.FullName == "Microsoft.VisualStudio.Design.VSTypeResolutionService") // Achieve instantaneous load, vs around a second's freeze
            {
                var normalEntries = (IEnumerable<object>)serviceType.GetField("_normalEntries", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(typeDiscoveryService);
                // ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions