Skip to content

NullReferenceException in PathBinder.AccessMember when instance implements IDictionary<,> explicitily #239

@oopee

Description

@oopee

PathBinder.AccessMember tries to find method ContainsKey via reflection for instances that implement generic IDictionary<,> interface. However, at the moment this throws a NullReferenceException if the instance implements ContainsKey method using explicit interface implementation.

Is there any reason to call GetMethod for the concrete instance type instead for the dictionary interface type? It seems that replacing

// Lines 287 - 290 in PathBinder.cs
if ((bool)instanceType.GetMethod("ContainsKey").Invoke(instance, new[] { key }))
{
    return instanceType.GetMethod("get_Item").Invoke(instance, new[] { key });
}

with

if ((bool)iDictInstance.GetMethod("ContainsKey").Invoke(instance, new[] { key }))
{
    return iDictInstance.GetMethod("get_Item").Invoke(instance, new[] { key });
}

would fix the issue.

Obviously this might break some code in some rare cases (e.g. a class which is derived from Dictionary and which provides a new (non-virtual) implementation for ContainsKey).

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