StackOverflowException when accessing member of nested class via a dynamic reference

后端 未结 2 1707
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 07:39

I have defined a generic class that derives from BindingList and has a nested non-generic class:

class Generic : BindingList.Inn         


        
2条回答
  •  无人及你
    2020-12-29 08:23

    Thank you very much for your correction! I investigated this I would say very interesting moment and found that I was right.

    First of all, this is not a BUG! This is just how the Microsoft team solved this issue. Again all what I wrote above I believe is true!

    So as I said you end up with an infinitive loop and get StackOverflow, but it seems to me that you get it very very fast. So no any long periods when you have no any access to your machine and just it looks like it's dead. I started digging deeper into the structure of BindingList and here the results.

    I created

    class Example : Level1>
    {
        public string Name = "111";
    }
    
    public class Level1
    {
    
    }
    

    and in the main

    dynamic d = new Example();
    var value = d.Name;
    

    and it works! Then I added another level

    public class Level1 : Level2
    {
    
    }
    
    public class Level2
    {
    
    }
    

    and I got StackOverflow. I changed to

    public class Level1 : Level2
    {
    
    }
    
    public class Level2
    {
    
    }
    

    and it works again!

    So I think that the guys from Microsoft just said ... so this is the max level after no way through and throw the exception.

    Now let's look at BindingList

    public class BindingList : Collection, 
        IBindingList, IList, ICollection, IEnumerable, ICancelAddNew, 
        IRaiseItemChangedEvents
    

    Notice Collection

    And look at List

    public class List : IList, ICollection, 
        IList, ICollection, IReadOnlyList, IReadOnlyCollection, IEnumerable, 
        IEnumerable
    

    Just interfaces....

    Therefore it works with List but not with BindingList!My example proves that!I believe they did it intentionally to stop infinitive looping.

提交回复
热议问题