Fast Dynamic Property Access in C# using Reflection.Emit

by Tobias Hertkorn on May 31st, 2006

I just read an extremely interesting article on CodeProject called: Fast Dynamic Property Access with C# by James Nies. Wow, I am really impressed both by the idea to use Emit in this genius way and by the coding style. Very nice work.

So, I had to sit down and port it to .NET 2.0 and use Generics. Actually, what I found out – it is way simpler, than what he did. ;) How fortunate for me, since this was the very first time I even looked at Emit.

Please remember: This is a quick port. It won’t work, if you don’t know exactly the return type, especially if it is a ValueType.

Here are links to download the stuff:

GenericPropertyAccessor as Zip

Or as txts:
And finally the updated test app:

This is not the complete solution you need in order to build the SampleApp. Please go to Codeproject and download the source code, then add my files.

May 31st, 2006 2:51 am | Comments (2)

Code Smell – Database style

by Tobias Hertkorn on May 29th, 2006

I just found a book on Amazon that seems to be the database based equivalent to
Martin Fowler’s book. It is called Refactoring Databases : Evolutionary Database Design (Addison Wesley Signature Series) and I just can’t wait to get it. If it’s as well written as Fowler’s book, it will be a great source of information about database design. I don’t know about you, but I tend to learn more by reading and understanding bad examples and seeing how somebody fixes those examples.

You will definitifly hear more about it as soon as I’ve got it in my hands. But the comments on amazon are quite promising.

May 29th, 2006 4:48 pm | Comments Off

On the road again

by Tobias Hertkorn on May 25th, 2006

For all of you waiting for the continuation of my previous partial post about custom attributes and reflection. Well, hopefully it will be done on Monday. I’ll be spending a LOT of time in trains the next 4 days. And that should create enough “spare” time for me to finish my work on a hopefully informative article about custom attributes, reflection and setting values dynamically at runtime. So stay tuned and be sure to check back on Tuesday. ;)

May 25th, 2006 12:35 am | Comments Off

What is “Code Smell”

by Tobias Hertkorn on May 20th, 2006

I bought Fowler’s Refactoring a couple of month back. And I just love going back to it and reading chapters again and again. One of the most interesting phrases used in the book is “Coding Smell”. And it is way easier to migrate legacy code, when your code is refactored and smells as little as possible. ;)

Today I stumbled across an excellent post concerning Coding Smells at Coding Horror.

To give you a short overview of what Coding Smells are – here is a quote from wikipedia:

In the community of computer programming, code smell is a jargon term used among programmers to refer to a symptom that indicates something may be wrong. It generally indicates that the code should be refactored or the overall design should be reexamined. The term appears to have be coined by Kent Beck on WardsWiki. Usage of the term increased after it was featured in Refactoring. Improving the Design of Existing Code

Determining what is and is not a code smell is often a subject judgment, and will often vary by language, developer and development methodology.

Common Code Smells:

  • Large method – a method, function, or procedure that has grown too large.
  • Large class – a class that has grown too large.
  • Feature envy – a class that uses methods of another class excessively.
  • Inappropriate intimacy – a class that has dependencies on implementation details of another class.
  • Refused bequeath – a class that overrides a method of a base class’ such that the contract of the base class is not honored by derived class. See Liskov substitution principle.
  • Lazy class – a class that does too little.

Some links to articles about “Code Smell”:

May 20th, 2006 12:23 pm | Comments (1)

Migrationpaths from legacy Visual Basic 6.0 to .NET

by Tobias Hertkorn on May 18th, 2006

There are a lot of problems converting a VB 6.0 app to .NET. So in a real life-time situation it is mostly not cost-effective to try and migrate everything in one step. Plus the programmers working on the project have to be trained in the new .NET language, C# or whatever. That calles for a slower approach. I call it Cross-Language Refactoring. The idea is to slowly replace performance critical or unmaintainable parts, while already implementing new features using the new language. Usually those new features interact with legacy code as well. So while the team is implementing a new feature, there is a chance to migrate those parts in the legacy code as well. This way the legacy code gets replaced bit by bit into the new object oriented world, while giving the team the time to get better at the new language.

I have a bit over 1 year experience in moving slowly from ASP to ASP.NET. There are a lot of problems there as well – but that would be a different article. Please feel free to contact me, if you need some pointers for that scenario. But this post will concentrate on moving a client app to .NET. Or at least start to do so.

There are two possible “slow” migration paths:

  1. Building a .NET GUI and integrating the legacy VB 6.0 through dlls
  2. Using the legacy interface and start replacing legacy modules by .NET modules

There are pros and cons to both approaches. The problem with the first migration path is, that it will use a lot of time to create a new GUI. On the other hand the second path will allow the user to still enjoy the old GUI and the old look-and-feel. But it is not an option when your GUI it too messed up, or is the reason why there is the need to migrate in the first place.

The MSDN-Magazin in May 2006 features an interesting article on how to make the second path possible:

Call Into The .NET Framework From Existing Visual Basic 6.0 Apps

Make sure to check it out before you are considering to migrate an application from VB 6.0 to .NET. Maybe a slow Cross-Language Refactoring approach is right for you as well.

On a different note: I would advise you to NOT move from VB 6.0 to VB.NET, but instead from VB 6.0 to C#. It turns out the programmers learning the new concept of object oriented programming, are way better at producing good code, when having a visual difference between the languages. When using VB.NET it is too tempting to use the old VB 6.0 “style” of programming and therefor neglecting patterns and oo design principles.

May 18th, 2006 3:15 pm | Comments (2)

Data paging and sorting

by Tobias Hertkorn on May 17th, 2006

I got interested in paging and sorting on large tables. And I found a couple of very good pointers, that I wanted to share with you:

A LOT to read. ;) Does anybody have more pointers? I am especially interested in anything related to the ObjectDataSource.

May 17th, 2006 11:03 am | Comments (5)

Java and Sun in the news today

by Tobias Hertkorn on May 16th, 2006

I am still a subscriber of the Debian Java mailing list. And on Tuesday a little, innocent email came over the mailing list:
Tom Marble – Towards Java Libre.

After that there was a bit of chit-chat, until reply email from Arnaud Vandyck. After that the thread is really taking off, inspiring very interesting posts about Open Source ethics, Sun’s need to adapt, etc. I encourage everyone to follow this thread!

On the same note:
Taken from two articles at heise (in German):
Wird Java doch Open Source?
Sun stellt Java Enterprise Edition 5 fertig

Sun thinking about releasing Java as Open Source
In a recent keynote Sun-CEO, Jonathan Schwartz, did publicly discuss the possibility that Sun might release Java as open source. It would be quite an agressive move to ensure predominance in the Linux world, since releasing Java as open source would create the possibility that it will be bundled with distributions. Right now for example Debian does not ship Sun’s Java through its main distribution mechanisms since it violates their distribution policies.

Sun releases Java Enterprise Edition 5
Java EE 5 should make it easier and faster to develope applications. It heavily uses annotations, which make it easier to handle EJB configuration, since the required parameters can be set within the code.

May 16th, 2006 8:01 pm | Comments Off

Using a more accurate approach to Timing

by Tobias Hertkorn on May 16th, 2006

As of .NET 2.0 there is a new class called StopWatch in System.Diagnostics. It is very easy to use. I just found it, while playing around with my Md5 optimization "project".

For all of you that can't upgrade to Framework 2.0 yet - here is the implementation I used to time my md5 algos. It is loosely based on the high performance timer Daniel Strigl posted on codeproject. I enhanced it a little bit in order to be able to use it running Mono on Linux. In that case it defaults to DateTime.Tick accuracy.

  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  5. namespace Md5Optimized
  6. {
  7.     public class PerformanceTimer
  8.     {
  9.         public static readonly bool IsHighPerformance;
  11.         [DllImport("Kernel32.dll")]
  12.         private static extern bool QueryPerformanceCounter(
  13.                 out long lpPerformanceCount);
  15.         [DllImport("Kernel32.dll")]
  16.         private static extern bool QueryPerformanceFrequency(
  17.                 out long lpFrequency);
  19.         private long m_startTime;
  20.         private long m_stopTime;
  21.         private static long m_freq;
  23.         static PerformanceTimer()
  24.         {
  25.             try
  26.             {
  27.                 IsHighPerformance =
  28.                    QueryPerformanceFrequency(out m_freq);
  29.             }
  30.             catch (Exception)
  31.             {
  32.                 IsHighPerformance = false;
  33.             }
  35.         }
  37.         public PerformanceTimer()
  38.         {
  39.             m_startTime = 0;
  40.             m_stopTime = 0;
  41.         }
  43.         /// <summary>
  44.         /// Start the timer
  45.         /// </summary>
  46.         public void Start()
  47.         {
  48.             // let the waiting threads do their work
  49.             Thread.Sleep(0);
  51.             if (IsHighPerformance)
  52.             {
  53.                 QueryPerformanceCounter(out m_startTime);
  54.             }
  55.             else
  56.             {
  57.                 m_startTime = DateTime.Now.Ticks;
  58.             }
  59.         }
  61.         /// <summary>
  62.         /// Stop the timer
  63.         /// </summary>
  64.         public void Stop()
  65.         {
  66.             if (IsHighPerformance)
  67.             {
  68.                 QueryPerformanceCounter(out m_stopTime);
  69.             }
  70.             else
  71.             {
  72.                 m_stopTime = DateTime.Now.Ticks;
  73.             }
  74.         }
  76.         /// <summary>
  77.         /// Returns the duration of the timer
  78.         /// (in fraction of seconds)
  79.         /// </summary>         
  80.         public double DurationSeconds
  81.         {
  82.             get
  83.             {
  84.                 if (IsHighPerformance)
  85.                 {
  86.                     return (double)(m_stopTime - m_startTime) /
  87.                         (double)m_freq;
  88.                 }
  89.                 else
  90.                 {
  91.                     TimeSpan span =
  92.                         (new DateTime(m_stopTime)) -
  93.                         (new DateTime(m_startTime));
  94.                     return span.TotalSeconds;
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }

Please keep in mind that this class is a quick hack. It works... ;) but only if used correctly.

Download PerformanceTimer.cs

May 16th, 2006 4:45 pm | Comments (9)

Human Rights Council election results

by Tobias Hertkorn on May 12th, 2006

On the same note as my post on baiku - how did this happen: UN Human Rights Council election results. China, Cuba, etc. Damn, they know for sure what human rights are...

May 12th, 2006 12:29 pm | Comments Off

Baiku Baidu – nothing like Wikipedia

by Tobias Hertkorn on May 12th, 2006

That's what they call "open" in China. Well, as you probably know, Wikipedia is banned in China due to its political content. Now they claim that the IDEA is not banned. Or is it? China's biggest search engine Baidu is creating an online enzyclopedia baiku like wikipedia, but I think it is nothing like wikipedia.

Here are the main differences:

  • Articles may only be published, if they do not offend the Censorship of Publications Board
  • It is illegal to publish any criticism against the functionaries and administration, hurt the public order, etc.
  • Baidu alone is judge what is appropriate and what is not.
  • Baidu owns all the legal rights to every article published within their system.
  • etc...

Do I have to go on any further? Sad, sad ...

May 12th, 2006 12:07 pm | Comments Off
Tobi + C# = T# - Blogged blogoscoop