Skip to content

Invalid 'REFL041 Delegate type is not matching expected' when there is not enough info to check this #231

Open
@jnm2

Description

@jnm2

Assembly referenced through a binary (compiled) metadata reference:

internal interface IFoo
{
    object Bar { get; }
}

public class Foo : IFoo
{
    internal object Bar { get; }

    object IFoo.Bar => Bar;
}

Assembly with analyzer gives erroneous REFL041. This is probably because it can't see internal members, so it makes the invalid assumption that GetProperty must be targeting IFoo.Bar rather than the Foo.Bar that may or may not be there:

class C 
{
    void M()
    {
        Delegate.CreateDelegate(
            // REFL041 Delegate type is not matching expected Func<IFoo, object>
            //     ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
            typeof(Func<Foo, object>),
            typeof(Foo).GetProperty("Bar", BindingFlags.NonPublic | BindingFlags.Instance).GetMethod);
    }
}

However, this form does not give REFL041. Potentially a second bug where these two forms are not seen as equivalent:

class C 
{
    void M()
    {
        Delegate.CreateDelegate(
            typeof(Func<Foo, object>),
            typeof(Foo).GetMethod("get_Bar", BindingFlags.NonPublic | BindingFlags.Instance));
    }
}

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