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:
- Set a breakpoint
- Setup a condition for it, which is a 'fake' one, but it gonna change the variable's value
- run in debug mode
- 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)
{
开发者_JS百科 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?
精彩评论