Showing posts with label VisualStudio. Show all posts
Showing posts with label VisualStudio. Show all posts

Saturday, 27 July 2013

Visual Studio's Make Object ID

Ever wished you could watch the value of an out-of-scope variable while debugging in Visual Studio? You can, if you use the Make Object ID option in the watch/local window. Here's an example, with a variable p that goes out of scope when control jumps to a new method, or back to the calling method:
class Person
{
    public Person(string name)
    {
        Name = name;
    }
    public string Name { get; set; }
}

public class Program
{
    public static void Main(string[] args)
    {
        DoIt();
    }

    private static void DoIt()
    {
        var p = new Person("Baldrick");
        DoIt2();
    }

    private static void DoIt2()
    {
        return;
    }
}
If you step through this code in Visual Studio and add a watch to variable p in method DoIt, the watch window will look like this:


When you step into method DoIt2, or out to Main, and refresh the watch on p, you get this message in the watch window:


If you want to be able to reliably watch the variable, the solution is to Make Object ID. Let's take the code back to DoIt and right-click on the watch on variable p:

Click on Make Object ID, and you'll see variable p is allocated object ID "1#"

If you add a watch to 1#, this allows you to view the value of p even when p goes out of scope:

The 1# reference can be used to update the value of the underlying variable p:

which will be reflected when (if) the object comes back into scope:



What about Garbage Collection?

The Object ID doesn't create a managed reference to the underlying object, which means that it will not interfere with its garbage collection:




Friday, 7 December 2012

Suppress the Visual Studio splash screen

If you start Visual Studio and it shows this:
You can suppress it by adding /nosplash to the command line in the shortcut used to launch Visual Studio:




Voilá!

Saturday, 24 November 2012

SignalR Demo In Pictures

Disclaimer: this demo is not intended to show best practice in C#, javascript, HTML, MVC, Visual Studio, NuGet etc etc. It is intended to use as a picture-book style guide to creating RPC communications between a web client and an ASP.Net application using SignalR.
Note: the code is not as simple as I'd hoped, but it changed to use a singleton to hold the timer, after the lack of best practice with a SignalR hub was pointed out by one of its authors.























Thursday, 13 September 2012

Treat Warnings as Errors: Exclude specifics warnings

As every Visual Studio user knows, you should build your project with the "Treat warnings as errors" switched on. This gives you very quick feedback when your project has problems that could otherwise get ignored.

This setting is toggled on the Build tab of the project's properties page:

As it says on the tin, this causes any problem that would normally be reported as a warning to be reported as an error, and prevent the project building:

If there are warnings that you don't want to break your build, you can suppress them specifically. You'll need the warning number, which will be reported in the Output window:

This number, without the CS prefix, needs to be entered in the "Suppress warnings" box, on the Build tab. Multiple warnings can be suppressed, by separating multiple warning codes with a comma.

Victory!

Alternatively, you can use a #pragma compiler directive in the code to suppress your warning:



Saturday, 25 August 2012

Project Template Locations in VS2012

Adding a Project Template 

If you've seen my post on creating a console application in VS Express 2012 for Web, you'll be thinking "why doesn't he create a template?" Well I did, via the Export Template Wizard in visual studio (File -> Export Template...)

This wizard creates a zip file of all the items in the current project. When this is copied to the correct location on your machine, it can be used to create a new project with all your previous goodness. The location to copy it to is [Visual Studio 2012 folder]\Templates\ProjectTemplates\Visual C# or[Visual Studio 2012 folder]\Templates\ProjectTemplates\Visual Basic

This allows you to select your template in the New Project dialog box

Specifying the Project Type


If you want the template to appear under one of the project types, e.g. Windows, put the .zip file into a directory of that name, e.g. [Visual Studio 2012 folder]\Templates\ProjectTemplates\Visual C#\Windows

Can I Have it in Both?

If you want it in both locations, Visual C# and Windows, you need to edit your template. Open the .zip file and there'll be a .vstemplate file; this is the definition of the project template. You need to add an element NumberOfParentCategoriesToRollUp. This tells VS to show the template in the project subtype (Windows) and its parent (Visual C#). Here's an example:

Creating a Console App in Visual Studio Express 2012 for Web

I've installed and am using Visual Studio Express 2012 for Web as my primary IDE for C#. There isn't a template for a console application, so if you want 1980s goodness, create a new class library and on the Properties page, setting Output Type to Console Application, like this

This then allows you to create a class with a Main method

and run the app with a push of F5

**UPDATE** I have created a project template that creates a console application, it's available on github, here with instructions on how to install it.


Sunday, 19 August 2012

Creating a VisualizerObjectSource

I did a quick introduction talk on Visual Studio Debugger Visualisers the other day, and one question I was asked was [something like] "Do you have to serialise the object to be visualised? Could you pass it to the visualiser and have it read the properties via reflection?"

The answer is yes and no. You do have to serialise the object to a stream, but you can implement your own method of serialisation. I'll try to explain that; the data to be visualised must pass between the process boundaries of the running process and the debugger process via a System.IO.Stream. The contents of this stream can be whatever you want, as long as the visualiser knows how to interpret it. Here's an example:

** If you haven't seen debugger visualisers before, check out my previous post here to give some background **

This is the object to be visualised:


Note the second argument to the DebuggerVisualiser attribute - this is vital to ensure that the correct class is used to provide the object to be visualised.

As the object is not marked as serialisable, it can't be provided to the visualiser via the normal GetObject method of the IVisualObjectProvider (the GetObject method relies on serialisation). However, what you can do is create a custom implementation of VisualizerObjectSource, to be used by the objectProvider. VisualiserObjectSource has a virtual method, GetData, with two parameters. The first parameter is an object, which will be the object to be visualised. The second parameter is a stream, which needs to be populated with the visualisation data for the object:


In this example, I'm passing an integer which represents the Argb value of the object's colour.

When the Show method on the visualiser is called, it's passed an instance of IVisualizerObjectProvider. With XML serialisable objects you would normally obtain the object to be visualised via the objectProvider.GetObject method. When the object is not serialisable, you need to call the GetData() method instead; the objectProvider calls the GetData method of the VisualizerObjectSource, and returns the contents of its outgoingStream argument. This can then be used to visualise the object:


When this is all put together, the visualiser looks like this:



The code from this example is on GitHub: https://github.com/orangutanboy/Visualiser-Demo-With-Custom-ObjectSource