.NET – missing methods – LINQ

Sometimes it makes me wonder why .NET doesn’t have some methods that really should be there.
string.IsNullOrWhiteSpace() just showed up in .NET 4 – should’ve been there ages ago.
For whatever reason the static extension methods aren’t allowed (nor extension properties.. come on, those _are_ methods.. Give’em to me!), so defining your own is not an option.
That is, I can create a static StringEx class full of utility methods, but extensions just look cleaner.

Anyways, one method LINQ is certainly missing is ToList() on old, non-generic IEnumerable.

        public static List<T> ToList<T>(this IEnumerable enumerable)
        {
            if (enumerable == null) throw new ArgumentNullException("enumerable");
            List<T> list = new List<T>();
            foreach (var item in enumerable)
            {
                T obj = (T)item; 
                list.Add(obj);
            }
            return list;
        }

Simple as that. I have to declare a resulting type on a method, so what. In VS extensions development, where a lot of properties are IEnumerables – this method sees a lot of use. And yeah, once it’s a list (or you could make it array, or whatever) – I can LINQ-query it, yeay!

You can see this method in use in my previous post – How to list all ProjectItems in Project   

 

EDIT:

Actually, I was wrong. The code above behaves very much like a Cast method from LINQ. Either Cast() or OfType() methods should be used.

How to list all ProjectItems in Project

If you’re looping through the project items (Project.ProjectItems) hoping to get a list of all the files at the root – you’re probably going to be dissapointed, because you’ll be missing all the code-behind files.
Turns out the code-behind files are in turn ProjectItems of a top-level file.
So basically if you want to get a list of all files in a (root directory of a) project, you need to check if the physical files (ProjectItem.Kind = {6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}) have a non-null ProjectItems property,
and pull those in as well.

This is the code I’ve used:

// project is of type Project here, but it could be a ProjectItem too.
List<ProjectItem> pli = project.ProjectItems.ToList<ProjectItem>();
List<ProjectItem> pfiles = pli.Where(x => x.GetKind() == ProjectItemKinds.PhysicalFile && x.ProjectItems.Count > 0).ToList();
foreach (var item in pfiles)
{
    pli.AddRange(item.ProjectItems.ToList<ProjectItem>());
}

Keep in mind it’s using extension methods of my own, but it’ll give the idea.

Implicitly initialized dictionaries

Dictionaries can be implicitly initialized just like lists:

Dictionary<int, string> map = new Dictionary<int, string>()
{
    { 1, "one" },
    { 2, "two" }
};

Can be useful when you have a static map of values, for example.. It can be initialized in a static constructor, of course, but this can be cleaner.

For whatever reason VS doesn’t autoformat it, btw.

How to reset experimental instance of Visual Studio

When developing Visual Studio extensions, they’re deployed to experimental instance, and can be annoying if you don’t want them anymore.

You can either uninstall them via Extension Manager, or reset the experimental instance using CreateExpInstance command.

For me (I’m running VS2010 SP1) the complete command is 

"C:Program Files (x86)Microsoft Visual Studio 2010 SDK SP1VisualStudioIntegrationToolsBinCreateExpInstance.exe" /Reset /VSInstance=10.0 /RootSuffix=Exp

 

Source