Friday, October 29, 2010

Service provider is missing the INameResolver service - WPF error

I got this error when I tried to use the new WPF 4 x:reference feature in visual studio 2010. Apparently, it is one of the VS 2010 XAML designer's glitches. Stop banging your head on the desk trying to figure out what went wrong and ignore this error.

Here is the explanation from Adam Nathan's WPF 4 unleashed book:
"The x:Reference markup extension is often mistakenly associated with the XAML2009
features that can only be used from loose XAML at the time of this writing. Although
x:Reference is a new feature in WPF 4, it can be used from XAML2006 just fine as long as your project is targeting version 4 or later of the .NET Framework.
One glitch is that the XAML designer in Visual Studio 2010 doesn’t properly handle x:Reference, so it gives the following design-time error that you can safely ignore:
Service provider is missing the INameResolver service"





Tuesday, October 26, 2010

C# Object vs Var vs Dynamic primitive data types

using System;
using System.Linq;

// Object Vs Var Vs Dynamic keywords in C#
class Program
{
public static void Main()
{
// Object
object objWhat = new Char[] { 'k', 'u', 'm', 'a', 'r' };
Console.WriteLine((char[])(objWhat)); // Strongly typed

// Var
var what = new Char[] { 'k', 'i', 'r', 'a', 'n' };
Console.WriteLine(what); // Strongly typed

// Dynamic
for (Int32 demo = 0; demo < 2; demo++)
{
var arg = (demo == 0) ? (dynamic)5 : (dynamic)"A"; // not known till tun time - weakly typed
var result = Plus(arg);
M(result);
}

Console.ReadLine();
}

private static dynamic Plus(dynamic arg) { return arg + arg; } // dynamic method
private static void M(Int32 n) { Console.WriteLine("M(Int32): " + n); }
private static void M(String s) { Console.WriteLine("M(String): " + s); }
}

// Output looks like this
// kumar
// kiran
// M(Int32): 10
// M(String): AA

// dynamic code taken from: Jeffrey's book CLR via C#