简体   繁体   中英

Visual Studio debugger automatic variable assignment

Yes, I see the other topic: Visual Studio Debugger - Automatic Variable Assignment

But I needed a solution today, and found one before I see it, and I'm wondering if is there a better one out there?

My case: I have a list of entities and I set up some policy related property based on a lot of factor. But it's not implemented yet or simply I want to test the UI when the entity has a right and when not (change it as fast as possible to do the real job).

So I set up the entity as if it has the right, so I can test the UI with this. But I don't want to break at every element in the list, and change the flag from true to false (to test the other case). I looked for a way to change a variable from debugger automatically. I came up with this solution:

  1. Set a breakpoint
  2. Setup a condition for it, which is a 'fake' one, but it gonna change the variable's value
  3. run in debug mode
  4. if I need the case 1. I enable the breakpoint, if I need the other one, I disable the breakpoint

Simplified example:

namespace AutoVariable
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                new Program().Entrance();
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }

        public void Entrance()
        {
            var entities = new List<Entity>
            {
                new Entity() { Name = "A" },
                new Entity() { Name = "B" },
                new Entity() { Name = "C" }
            };

            entities.ForEach(setRight);
            entities.ForEach(Console.WriteLine);
        }

        protected void setRight(Entity entity)
        {
            //not implemented
            bool hasRight = true;
            entity.HasRight = hasRight;
        }
    }

    class Entity
    {
        public bool HasRight { get; set; }

        public string Name { get; set; }

        public override string ToString()
        {
            return string.Format("{0} - {1}", Name, HasRight);
        }
    }
}

I set up a condition breakpoint to: entity.HasRight = hasRight;

with this condition: (hasRight = false)

so the hasRight will be false and the breakpoint never got a hit.

But it can be used in other cases also, for example in Jason Irwin's post, you can use something like: (userName = "Jason").Length < 1

So my question that is it a good solution or I am missing something from the 'native' debugger toolset?

Thanks in advance!

negra

You want to do an action using the debugger right? There's a thing called Trace Points.

It is explained here: http://weblogs.asp.net/scottgu/archive/2010/08/18/debugging-tips-with-visual-studio-2010.aspx and go down to "TracePoints – Custom Actions When Hitting a BreakPoint"

Is that what you need?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM