Tag Archives: Tools

ReSharper and code generation

I don’t know why I’ve found this feature only today, but I wanted to share another great feature of ReSharper. Let say you need to implement a C# structure. You may start with the following code:

public struct ServerItem
{
    public string Id { get; private set; }
    public DateTime? Added { get; private set; }
    public int ChildCount { get; private set; }
}

Then you start thinking, “I need to setup a constructor…” You can do it manually, but you can also ask ReSharper do to the job for you. All you have to do is press ALT+Enter (this might depend on your configuration obviously…)

ReSharper will generate the constructor for you:

public struct ServerItem
{
    public ServerItem(string id, DateTime? added, int childCount)
        : this()
    {
        this.Id = id;
        this.Added = added;
        this.ChildCount = childCount;
    }

    public string Id { get; private set; }
    public DateTime? Added { get; private set; }
    public int ChildCount { get; private set; }
}

Fine. Then you remember that you also need to setup equality members properly… You have to override Equals, GetHashCode… This is not complicated but it can become cumbersome and it often feels like a waste of time. Here is the ReSharper way of doing this:

1. Press ALT+Enter

2. Choose “Equality members” and setup the code generation:

And BOOOM ! You’re done:

public struct ServerItem
{
    public ServerItem(string id, DateTime? added, int childCount)
        : this()
    {
        this.Id = id;
        this.Added = added;
        this.ChildCount = childCount;
    }

    public string Id { get; private set; }
    public DateTime? Added { get; private set; }
    public int ChildCount { get; private set; }

    public bool Equals(ServerItem other)
    {
        return Equals(other.Id, this.Id) && other.Added.Equals(this.Added) && other.ChildCount == this.ChildCount;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (obj.GetType() != typeof(ServerItem))
        {
            return false;
        }
        return Equals((ServerItem)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = (this.Id != null ? this.Id.GetHashCode() : 0);
            result = (result * 397) ^ (this.Added.HasValue ? this.Added.Value.GetHashCode() : 0);
            result = (result * 397) ^ this.ChildCount;
            return result;
        }
    }

    public static bool operator ==(ServerItem left, ServerItem right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(ServerItem left, ServerItem right)
    {
        return !left.Equals(right);
    }
}

The code-generation features of ReSharper has been there for a long time… But because I just found out the power of them, I wanted to briefly showcased them in this post 🙂

If you like typing XAML you will love ReSharper 6.1 !

Resharper is an amazing tool for any .Net developers. The latest version 6.1 has been released just a couple of weeks ago and I wanted to share with you a brief overview of the new workflow available in the XAML world !

Visual Studio 2010 introduced 2 new design time properties: d:DesignInstance and d:DesignData. Those properties can be used in order to specify a design time DataContext in order to have more help during the creation of a binding.

For example, when you create a binding using the Property dialog of VS2010 you can browse your DataContext to select the right property (image from this blog post from Karl Shifflet):

Resharper 6.1 is now able to use those metadata in order to improve the experience you have while typing XAML (which I personally do a LOT!). Here is how it works:

  • you create a new ViewModel with a simple property (this property has just get/set because we don’t need much more in the context of this post…)

  • you setup a binding in your view

At this point the ReSharper magic comes into play…

  • ReSharper warns you the DataContext is unknown

  • Offer the ability to fix this

  • Note that like in C#, you can very easily resolve namespace issues

  • Then notice that the warning is gone (the Title property is no longer underlined)

  • You can now add a new binding

  • You can then ask ReSharper to create the property in your ViewModel

  • Choosing the first option will get you to the ViewModel definition

Now that I’ve upgraded my installation to version 6.1, I think this is a must have !

That’s all for today ! Hope it helps 🙂

 

 

Using Reflector to debug a .Net app in Visual Studio without the original source code

Many .Net developers use to say “if you’re a serious developer, then you MUST Reflector”.

I would like to add another statement: “if you want to have an insight and powerful look at how things works internally, use Reflector Professional”.

Last Wednesday, RedGate released a new version of Reflector. You’re probably going to download it soon or later because your actual version is going to expire. When you’ll download the free version, you’ll automatically get a trial (14 days) of the professional edition. Nice, but what’s so special about this edition ?

Let’s see a demonstration of what can be done using Reflector Pro. The following is not an ad for RedGate, I’m just totally amazed by their new feature 🙂

1. Reflector is now integrated into Visual Studio (2005, 2008 and 2010 RC):

2. Select the “Choose Assemblie to Debug…” option to select .Net assemblies for which you don’t have the source code. In this example, I’m using one of the Blend3’s assemblies:

3. Once the process is completed, select the “Explore Decompiled Assemblies” option:

4. Browse to your target assembly and select an interesting type:

5. Put a breakpoint in the code, like you do every day when you debug an app:

6. Run the executable

7. Debug Blend3’s source code ! Use breakpoints, step into methods, inspect variables…

And that’s it. With Reflector Professional, you can:

  • Decompile third-party assemblies from within Visual Studio
  • Step through decompiled assemblies and use all the debugging techniques you would use on your own code. This is incredibly powerful as we saw by debugging Blend3″s source code !

Reflector Professional is available for about 195$ on RedGate website