Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DLR does not respect InternalsVisibleTo for anonymous types #15790

Closed
natemcmaster opened this issue Nov 25, 2015 · 5 comments
Closed

DLR does not respect InternalsVisibleTo for anonymous types #15790

natemcmaster opened this issue Nov 25, 2015 · 5 comments
Assignees
Milestone

Comments

@natemcmaster
Copy link
Contributor

In full .NET, anonymous types are accessible via DLR across assembly boundaries if an InternalsVisibleTo attribute is present. This behavior is not the same in CoreCLR. Instead, a RuntimeBinderException is thrown.

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'object' does not contain a definition for 'xxxx'

Consider the following scenario:

In assembly ConsoleApp:

    public class Program
    {
         public static void Main()
         {
            var list = new List<object>();
            list.AddGreeting("Hello world");
            dynamic item = list[0];
            Console.WriteLine(item.Greeting); // throws in CoreCLR. Prints "Hello world" in full .NET
         }
     }

In assembly GreetingLibrary:

[assembly: InternalsVisibleTo("ConsoleApp")]

public static class ListExtension
{
    public static void AddGreeting(this List<object> source, string greeting)
    {
        source.Add(new { Greeting = greeting });
    }
}

In .NET 4.5.1, this console application will print "Hello world" but in CoreCLR this application throws an exception.

Full repro project:
test-dlr-bug-master.zip

@Eilon
Copy link
Member

Eilon commented Nov 25, 2015

Note: This is blocking some EntityFramework scenarios.

@natemcmaster
Copy link
Contributor Author

Fortunately there simple workaround: just don't use anonymous types if they are potentially used outside the assembly. (See dotnet/efcore#3898)

@Eilon
Copy link
Member

Eilon commented Nov 26, 2015

@natemcmaster I would recommend avoiding using anonymous types entirely within our product codebase (in tests is fine). Anonymous types can produce many surprises.

@gkhanna79
Copy link
Member

@jaredpar This looks like a Roslyn issue - Can you please route this?

@jaredpar jaredpar assigned VSadov and unassigned jaredpar Dec 3, 2015
@karelz
Copy link
Member

karelz commented Nov 28, 2016

The is by design - .NET Core reflection is more restrictive.

@karelz karelz closed this as completed Nov 28, 2016
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.0.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants