Tag Archives: vs11 beta

Tips

Caller info attributes in C# 5 are a compiler feature – they are framework agnostic

The new caller information attributes in C# 5 are excellent news for people tired of writing logging and diagnostic code or implementing INotifyPropertyChanged. You simply supply the correct attribute for the function parameters with default values and voilĂ  – you get information about the caller for free. Consider the following code:

using System;
using System.Runtime.CompilerServices;

class Program
{
    static void SayMyName([CallerMemberName] string functionName = "",
                          [CallerFilePath] string filePath = "",
                          [CallerLineNumber] int lineNumber = 0)
    {
        Console.WriteLine("{0}:{1}({2})", filePath, functionName, lineNumber);
    }

    static void Main(string[] args)
    {
        SayMyName();
    }
}

The output of this simple program is:

c:\Users\Toni\Documents\Visual Studio 11\Projects\test_console\cstest\Program.cs:Main(15)

Excellent, we have the caller’s information for free. .NET 4.5 Framework introduced the above used attributes: CallerMemberNameAttribute, CallerFilePathAttribute and CallerLineNumberAttribute. They are defined in the System.Runtime.CompilerServices namespace. This means that if you are using Visual Studio 11 and you are targeting earlier framework, you do not have those types defined in mscorlib.dll. However, even though this might make you think that you are left in the dark, this is a compiler feature which means that these attributes are nothing but a magic keywords for the compiler. They themselves do nothing!

In fact, I have compiled the above program targeting .NET 2.0 Framework, the earliest target type available in Visual Studio 11. The trick is to add the right “magic” attributes and it will work since the compiler will interpret them correctly. The full source code is rather short and you can find it as a gist at github.

I based this idea on a similar “hack” for using async in projects targeting .NET 4 on the C#5 compiler: https://gist.github.com/1961087.

TFS/ALM

Consuming OData for TFS using C#

This entry is part of a series, OData Service for TFS»

*UPDATED 26.3.2012 FIXED ExecuteAsync wrapper*

In the previous post we saw how to install and configure OData Service for TFS 11. Before we proceed with extending the service, let’s see how to consume it. For this example, we are going to build from scratch a simple console program using C#. Then we are going to fetch some data. We will be doing it in Visual Studio 11 in .NET 4.5.

Create a new Console Application project with in Visual C# templates and make sure that you target .NET Framework 4.5. Add new service reference and type in full path to OData Service, in my case the address is

http://localhost/ODataTFS.Web/DefaultCollection.

Note that the team project collection is included in the uri. Change default name for namespace to TFSData and click OK. Just before closing, the dialog should look similar to the image below. If you do not see IterationPaths in your dialog, do not worry, we will come to that part in our next posts. read more »

Tips

Range-based for-loop in VS11 Beta

One C++11 feature that didn’t made it in VS10 was range-based for-loop. Even though it wasn’t planned for VS11 release either, it is actually implemented. Even though the editor will draw red squiggles, it is not actually an error to write it (as seen on the screenshot below). The usage is fairly simple:

std::vector<int> v;
for(int i: v)
{
	std::cout << i << std::endl;
}

Hit F5 and compilation passes smoothly. The console windows displays our elements, proving that in fact it does work.