Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

How does VS inspect properties?

Back to Forum: Tech Off
  • User profile image
    W3bbo

    When debugging, the Locals/Autos/This windows show the values of object properties.

    I was wondering how this is possible, since properties are just two methods (Get and Set) rolled under a single alias.

    So unless VS calls each and every property of the object (which I doubt), how does it do it?

    And what if calling a property results in dataloss?

    For example, I could do this:

    Public Property DontReadThis
    Get
    Me.Dispose()
    Return True
    End Get
    End Property


    So if VS called it, data-loss would occur but VS wouldn't know this.

    So how does VS inspect property values?

  • User profile image
    bitmask

    The debugger does execute the code inside the property. It's easy to see with a class like the following, the _counter field will keep incrementing.

    class WatchMe
    {
        private int _counter;
        public int Counter
        {
            get { return _counter; }
            set { _counter = value; }
        }


        private int _someValue;
        public int SomeValue
        {
            get
            {
                _counter++;
                return _someValue;
            }
            set { _someValue = value; }
        }
    }

    Tools -> Options -> Debugging -> "Enable property evaluation..." controls this behavior. In 2.0 there is the DebuggerBrowsable attribute to apply if a property does something evil.

  • User profile image
    Yggdrasil

    bitmask wrote:
    The debugger does execute the code inside the property. It's easy to see with a class like the following, the _counter field will keep incrementing.


    Exactly. This is another good reason to avoid side-effects in Property Get methods. It forces you to be less evil. Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.