Bug 14762 - Dynamic keyword restricts access to internal members exposed via public interface
Summary: Dynamic keyword restricts access to internal members exposed via public inter...
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-09-15 04:36 UTC by rryk.ua
Modified: 2013-09-24 13:22 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description rryk.ua 2013-09-15 04:36:00 UTC
Description of Problem:
Dynamic keyword makes internal members hidden even if they are exposed with a public interface. Please see example below.

Steps to reproduce the problem:
1. Write an application as described below.
2. Run it.


Actual Results:
Access to members is restricted when object is dynamic.

Expected Results:
Access to members is not restricted when object is dynamic because these methods are exposed via a public interface.

How often does this happen? 
Always.

Additional Information:

Create a solution with two projects - a library and a solution with the following code:

    public interface IEntity
    {
        int Property { get; }
        string Method();
        string this[int index] { get; }
    }

    public class EntityFactory
    {
        public static IEntity CreateEntity() { return new Entity(); }
    }

    internal class Entity : IEntity
    {
        public int Property { get { return 42; } }
        public string Method() { return "bar"; }
        public string this[int index] { get { return "indexer"; } }
    }

and

    class Program
    {
        public static void Main(string[] args)
        {
            // This works fine.
            var entity = EntityFactory.CreateEntity();
            Console.WriteLine(entity.Property);
            Console.WriteLine(entity.Method());
            Console.WriteLine(entity[3]);

            // Each of the last three lines causes an exception, but shouldn't.
            dynamic dynamic_entity = EntityFactory.CreateEntity();
            Console.WriteLine(dynamic_entity.Property);
            Console.WriteLine(dynamic_entity.Method());
            Console.WriteLine(dynamic_entity[3]);
        }
    }
Comment 1 Marek Safar 2013-09-24 13:22:33 UTC
Fixed in master.

The code is not really valid, dynamic code does not see how the object was created it only seems the actual instance which is in this case class Entity and that class is inaccessible.