RSS

Category Archives: Visual Studio

Adding Extension Methods To Every Object in C#

Extension methods in C# are great tools at some locations they are especially helpful when extending types that are not owned by the caller.
Extension methods provide three major advantages,
1. Centralizing common code by providing domain specific language.
2. Extending types without using inheritance or composition.
3. Extending external codebases.
If you need to extend every object how would you do it? Well you can extend “Object” type and “Type” classes since every object in C# inherits from “Object” type and “Object” type contains “GetType()” method which returns “Type”. This means extending “Object” and “Type” will propagate to all objects if you import the correct namespace.
In following implementation I am writing extension method to serialize any object to JSON format. This sort of implementations are very useful in crosscutting concerns like logging.

namespace System
{
	public static class ObjectExtensions
	{
		public static string ToJson(this Object obj)
		{
			return JsonConvert.SerializeObject(obj);
		}
	}
}

In above code I am extending Object type so that every object might be able to use provided that the assembly referenced and since I am defining this in “System” namespace that is the only requirement. If you define this in any other namespace then you have to import that namespace to use as an object extension.
Usage for the JSON convert is simple as shown below.

namespace Extensions.Tests
{
	[TestFixture]
	public class ObjectExtentionTests
	{
		[Test]
		public void Object_Extention_ConvertToJSON_Test()
		{
			var p = new Person() { FirstName = "Dimuthu", LastName = "Perera"};
			Debug.Write(p.ToJson());
		}
	}

	public class Person
	{
		public string FirstName { get; set; }
		public string LastName { get; set; }
	}
}

When you access intelisense for object, Visual Studio will show the extension method as an option in the list as shown below.
Extension method intelisense support for every object

So output will be when JSON serialized in above code will be as following,

{"FirstName":"Dimuthu","LastName":"Perera"}
Advertisements
 
Leave a comment

Posted by on February 28, 2014 in .NET, C#, Visual Studio

 

Tags: , ,

Using Default browser switcher in Visual Studio 2010

Default browser switcher is a really power tool visual studio plugin that can be installed which will enable you to switch default browser that your web application going to get opened when you run your web application through Visual Studio. Traditionally you have to right click on a web page in visual studio and click open with and then select the browser.

But with this cool visual studio extension that is just a one click away it will display nice little toolbar with browser icons.

You can easily search and download it from extension manager which is found in Visual Studio 2010 > Tools > extensions manager.

 
Leave a comment

Posted by on May 27, 2012 in Visual Studio

 

Visual Studio Debugger Canvas – Efficient way to see callstacks

If you are a .NET developer you must have done debugging..Traditionally when we do debugging its very source file centric which means you can set breakpoints in source files but when you stepping through your code you might easily loose overall picture if you have complex procedures functions calling here and there. This is really common if you are debugging very complex code that has a big callstack.

As the name suggests Debugger Canvas helps you to debug your code with kind of overall picture of your call stack in pictorial representation on a Canvas in mind. It helps you to understand bigger picture of your code while you are debugging.

Debugger canvas is actually a Visual Studio Plugin more information and you can download it from here or use visual studio extensions manager to download it

Here is the sample code which calculates factorial of a number which I used to test this plugin and see the debugger canvas output and traditional callstack view approach.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            var number = 8;

            Console.WriteLine("Factorial Of " + number + " Is " + GetFactorial(number ));

            Console.ReadLine();       
        }

        static long GetFactorial(long number)
        {
            long factorial = 1;
            for (int i = 1; i <= number; i++)
            {
                factorial = factorial * i;
            }
            return factorial;
        }
    }
}

Debugger Canvas

Image

Traditional Callstack view windows in Visual Studio 2010

Image

 
Leave a comment

Posted by on May 27, 2012 in Visual Studio

 

Tags: