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

fix index access bug caused by Access Modifiers of C# class #3554

Merged
merged 2 commits into from
Mar 18, 2020

Conversation

Danieladu
Copy link
Collaborator

@Danieladu Danieladu commented Mar 13, 2020

Close: #3555

Bug description: Assume there is a static class PublicA, and it contains a private class A, the structure shown as below:

public static class PublicA
    {
        public static object GetList()
        {
            return new List<A> { new A("hi") };
        }

        private class A
        {
            public A(string name)
            {
                this.Name = name;
            }
            public string Name { get; set; }
        }
    }

We call the static method in another class (which has no access to class A), and use dynamic as an intermediate variable. Code as below:

var list = PublicA.GetList();
dynamic dy = list;
var result = dy[0];

When running through the code, the error occurs with the message: Cannot apply indexing with [] to an expression of type 'object'

So, the root cause of the issue is that when we cast a list value (with a specific object Generics, and the caller has no access to this class) to dynamic, some features of List would be lost.

@Danieladu Danieladu requested a review from boydc2014 March 13, 2020 12:46
@Danieladu Danieladu added the LG label Mar 13, 2020
@johnataylor
Copy link
Member

Our guidance is to avoid using dynamic within our code.

Copy link
Member

@johnataylor johnataylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - perhaps wait for Tom's review too. Note for future - don't use dynamic in our code - its does not clarify.

@Danieladu Danieladu requested a review from tomlm March 14, 2020 01:28
@Danieladu Danieladu added the R9 Release 9 - May 15th, 2020 label Mar 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R9 Release 9 - May 15th, 2020
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Access index fails for list with object Generics
2 participants