CzechDeveloper.com

Jetbrains DotPeek - yet another disassembler?

After Reflector - for many years free and almost only to-code disassembler - started to charge for it's use, couple companies tried to take its place.

Most notably

All with by various degree offered Reflector functionality and were usable.

But by now, well known company JetBrains (creator of Resharper, TeamCity, YouTrack etc.) created their own, and by my judgement by far the best, disassembler.

DotPeek

Why is it the best?

Yes, it has clean design, all functions you need and fine disassembled code generation.

It has very cool navigation in code, ReSharper-like icons

But most notably, it allows you to easily combine source code from multiple sources. This means, that while you navigate in .Net classes, it automatically downloads code of .Net framework and presents you with real code.

So I can happily announce migration from ILSpy, which I used with 'meh, it also works' to to DotPeek, which I consider to be the best tool for .Net Assembly disassembling on current market.

PS: If you are interested in hacking/changing of assemblies and learning IL, there is always GreyWolf


One line password generation in .Net using LINQ

When I wrote this code in use-once application, I thought that is rather weird part of code, but works just fine for this once case.
Then I realized, there is nothing wrong with the code. People, who are familiar with LINQ will have little problem reading it and it will work 100% reliably.
Therefore I plan to reuse this code in the future.

Really in just 1 line you can generate random password from set of predefined characters. Advantage of predefined characters is, that you can remove characters that can cause problems. Because I live in Europe, I remove Z and Y as it may be in different places on keyboard base on keyboard layout. But you can also consider removing 0O or 1l combinations.

VB.NET

shared random as new Random()
Function GeneratePassword(lenght as Integer, allowedChars as String) as String
    return new String(Enumerable.Range(0,10).Select(Function(a) allowedChars(random.Next(allowedChars.Length))).ToArray())
end Function

C#

static Random random = new Random();
static string GeneratePassword(int lenght, string allowedChars) {
    return new string(Enumerable.Range(0,lenght).Select (a => allowedChars[random.Next(allowedChars.Length)]).ToArray());
}

Entity Framework Code First type safe eager loading.

In Entity Framework Code First, you can achieve eager loading of entities by using Include method on DbQuery in from DbContext of your application.

In contrary to normal old Entity Framework, in Code First you cannot use Lambda expression. Only way is to specify property name (or names) by string;

VB.NET

dbContext.MyEntities.Include("EntityRelationship")

C#

dbContext.MyEntities.Include("EntityRelationship");

This can cause some problems while refactoring solution.

If you don't mind performance penalty, you can write extension on DbQuery, which will transform lambda expression to string and use it in include. This way you will achieve type safety and better maintainability of code.

Then you can use Lambda expression instead of string.

VB.NET

dbContext.MyEntities.Include(Function(a) a.EntityRelationship)

C#

dbContext.MyEntities.Include((a) => a.EntityRelationship);

First Method extract names of property chain from lambda expression

VB.NET

Imports System.Linq.Expressions

Public Module ExpressionHelper

  Public Function GetPropertyName(Of T)(expression As Expression(Of Func(Of T, Object))) As String
    Dim memberExpr As MemberExpression
    Select Case expression.Body.NodeType
      Case ExpressionType.Convert, ExpressionType.AddAssignChecked
        memberExpr = TryCast(DirectCast(expression.Body, UnaryExpression).Operand, MemberExpression)
      Case Else
        memberExpr = TryCast(expression.Body, MemberExpression)
    End Select

    If memberExpr Is Nothing Then Throw New ArgumentException("Expression must be Member Expression")

    If memberExpr.Expression.NodeType = ExpressionType.Parameter Then
      Return memberExpr.Member.Name
    Else
      Dim result As New Text.StringBuilder
      While memberExpr IsNot Nothing
        result.Insert(0, "."c)
        result.Insert(0, memberExpr.Member.Name)

        memberExpr = TryCast(memberExpr.Expression, MemberExpression)
      End While
      result.Remove(result.Length - 1, 1)
      Return result.ToString()
    End If

  End Function

End Module

C#

public static class ExpressionHelper {
  public static string GetPropertyName<T>(Expression<Func<T, object>> expression) {
    MemberExpression memberExpr = default(MemberExpression);
    switch (expression.Body.NodeType) {
      case ExpressionType.Convert:
      case ExpressionType.AddAssignChecked:
        memberExpr = ((UnaryExpression)expression.Body).Operand as MemberExpression;
        break;
      default:
        memberExpr = expression.Body as MemberExpression;
        break;
    }

    if (memberExpr == null)
      throw new ArgumentException("Expression must be Member Expression");

    if (memberExpr.Expression.NodeType == ExpressionType.Parameter) {
      return memberExpr.Member.Name;
    }
    else {
      StringBuilder result = new StringBuilder();
      while (memberExpr != null) {
        result.Insert(0, '.');
        result.Insert(0, memberExpr.Member.Name);

        memberExpr = memberExpr.Expression as MemberExpression;
      }
      result.Remove(result.Length - 1, 1);
      return result.ToString();
    }
  }
}

Second Method creates extension on DbQuery, so it feels natural to apply it

VB.NET

Imports System.Data.Objects
Imports System.Linq.Expressions
Imports System.Runtime.CompilerServices
Imports System.Data.Entity.Infrastructure

Public Module ObjectQueryExtensions

  <Extension()>
  Public Function Include(Of T)(query As DbQuery(Of T), expression As Expression(Of Func(Of T, Object))) As DbQuery(Of T)
    Dim propertyName = ExpressionHelper.GetPropertyName(expression)
    Return query.Include(propertyName)
  End Function

End Module

C#

public static class ObjectQueryExtensions {  
  public DbQuery<T> Include<T>(this DbQuery<T> query, Expression<Func<T, object>> expression) {
    string propertyName = ExpressionHelper.GetPropertyName(expression);
    return query.Include(propertyName);    
  }
}

If you look closely on first method, you can notice it already contains some performance optimization. Usual case will consist of just one property include, therefore in that case I skip expensive StringBuilder creation and return property name directly.

I hope you will find this perk useful.


Tools I could not live without

I try not to overdo it with amount of tools I use, but I'll make a short list of tools I use right now.

Code writing

  • Visual Studio - I think this needs not further comment.
  • LINQ Pad - Very useful tool for quick code testing. Connect it to your DLLs and test everything right away. This should not replace unit tests, but it's very useful anyway. I sometimes use it instead of command line.
  • Notepad++ - Where Visual Studio is too big to run.
  • DotPeek - For my use the best disassembled on the market.
  • WinMerge - Simple text file comparison.
  • moq - Fast and easy stub and mock object creation (for unit testing).

Database utilities

Other Utilities

  • clipx - Clipboard history manager. Because as developer, you never have enough items in clipboard.
  • Papercut - Development SMTP server. It's must have if you create application that sends emails.
  • Silverlight Spy - For analyzing and simple test of Silverlight UI)
  • Snippet Editor - Not really extension. It helps you to maintain Visual Studio snippets. You use snippets, right?

Web Applications

For annoying registrations

  • BugMeNot - Find already existing accounts
  • 10 Mimute Mail - For quick registrations when you don't want to give your real email.

Misc

  • Paint.NET - Very simple to use image editor. Even as a programmer, you need one.
  • IrfanView - Not just image viewer, but simple image converter too.
  • Skype - For team communication.
  • VLC - You need to listen to some music, don't you :).
  • iReader - To get rid of clutter while reading.

Relaxation

Some of my own