May 192011
 

Ever since the announcement of a paid-for product, .NET Reflector by RedGate which has been a free application that is now an essential tool to many .NET developers, there has been an uproar among the community developers regarding this move to completely change a free essential developer tool to a paid-for app.

JetBrains, the creator of the famous and popular ReSharper, dotTrace and dotCover, has now created a free decompiler tool that’s similar (if not better) than .NET Reflector called dotPeek.

dotPeek offers:

  • Decompilation of .NET 1.0-4.0 assemblies to C#
  • Code syntax highlighting and code insight features
  • Complete keyboard support
  • Visual Studio look-and-feel
  • The full power of ReSharper-style navigation

Learn more about dotPeek and download an early build. Please submit bug reports and feature requests to the dotPeek issue tracker, and discuss the tool on the dotPeek forum.

For latest news and hints, follow dotPeek on twitter or like JetBrains on Facebook

Finally, have in mind that decompiling will also be available right in Visual Studio as part of the upcoming ReSharper 6.

Mar 112010
 

This is my little research today for some work I’m helping out. So you have a Unicode encoded HTML string (or url), e.g. “津津有味”, that you want to convert to an actual unicode string, in this case “津津有味”, but you don’t have the trusty use of HtmlUtility.HtmlDecode or anything on hand (for example you’re using the compact framework like myself). Here’s how you convert those numbers into actual unicode characters.

[sourcecode language="csharp"]
// Convert the number into a short – you can make this a little more safe by using short.TryParse instead
ushort mycode = Convert.ToUInt16("27941");

// Now convert that integer into a byte array
byte[] mybytes = BitConverter.GetBytes(mycode);

// We have our byte array, convert to a string! Tada!
string mystring = Encoding.Unicode.GetString(mybytes);
[/sourcecode]

And there we have it. In order to parse all the unicode encoded numbers out, you just need a simple regular expression which I’m sure you can figure out.

I hope that helps someone out there, because it took me some time inspecting variables in order to get it right.

Dec 152009
 

I thought I’ll post this in case anyone else encounters this problem. The purpose of the method is to get back a set of ClientContacts filtering based on the ClientID and select only those active if the onlyActive parameter is true, else ignore the IsActive filter and return all the records.

Here’s my LINQ query with a simple if-else shortcut within it:

[sourcecode language="csharp" highlight="5"]
public IQueryable<ClientContact> GetClientContactByClient(int clientID, bool onlyActive)
{
return from clientContact in this.db.ClientContacts
where clientContact.Client_AutoID == clientID &&
onlyActive ? clientContact.IsActive : true
select clientContact;
}
[/sourcecode]

Notice line 5? Here’s where I got this code wrong. The above code generates the following SQL statement:

[sourcecode language="sql"]
SELECT *
FROM [dbo].[ClientContacts] AS [t0]
WHERE (
(CASE
WHEN [t0].[Client_AutoID] = @p0 THEN CONVERT(Int,[t0].[IsActive])
ELSE @p1
END)) = 1
[/sourcecode]

If you actually work it out, this SQL statement will always return all records.

Here’s the change I made on the code to fix it.

[sourcecode language="csharp" highlight="5"]
public IQueryable<ClientContact> GetClientContactByClient(int clientID, bool onlyActive)
{
return from clientContact in this.db.ClientContacts
where clientContact.Client_AutoID == clientID &&
(onlyActive ? clientContact.IsActive : true)
select clientContact;
}
[/sourcecode]

Notice the brackets I added? This resulted in the generation of the following SQL statement:

[sourcecode language="sql"]
SELECT *
FROM [dbo].[ClientContacts] AS [t0]
WHERE ([t0].[Client_AutoID] = @p0) AND ((
(CASE
WHEN @p1 = 1 THEN CONVERT(Int,[t0].[IsActive])
ELSE @p2
END)) = 1)
[/sourcecode]

Again, if you work it out, this is the correct SQL statement to achieve what I want. I didn’t know a set of brackets make such a huge difference in the SQL code generation. Do take note of this when you’re finding out why your LINQ query doesn’t work as you might have thought.