Here is what I've added to the code to handle the BitmapSource cache and hopefully (yes, I have sined by not measuring anything, before or after) improve performance:

using BitmapSourceDictionary = System.Collections.Generic.Dictionary<int, System.Windows.Media.Imaging.BitmapSource>;

 

namespace VistaBridge.UI {

   public class StockIcon : MarkupExtension {

      static BitmapSourceDictionary _cache;

 

      static StockIcon() {

         _cache = new BitmapSourceDictionary();

      }

 

      public override int GetHashCode() {

         return ComputeUniqueValue( _identifier, _flags);

      }

 

      static int ComputeUniqueValue(StockIconIdentifier identifier, StockIconOptions flags) {

         int value = checked(((int) identifier) << 4);

         if ( (flags & StockIconOptions.Selected) == StockIconOptions.Selected)

            value += 8;

         if ( (flags & StockIconOptions.Small) == StockIconOptions.Small)

            value += 4;

         if ( (flags & StockIconOptions.LinkOverlay) == StockIconOptions.LinkOverlay)

            value += 2;

         if ( (flags & StockIconOptions.ShellSize) == StockIconOptions.ShellSize)

            value += 1;

         return value;

      }

 

      protected internal static BitmapSource GetBitmapSource(StockIconIdentifier identifier, StockIconOptions flags) {

         BitmapSource bitmapSource;

         int uniqueValue = ComputeUniqueValue(identifier, flags);

         if (!_cache.TryGetValue(uniqueValue, out bitmapSource)) {

            bitmapSource = (BitmapSource) InteropHelper.MakeImage(identifier, StockIconOptions.Handle | flags);

            bitmapSource.Freeze();

            _cache[uniqueValue] = bitmapSource;

         } else {

            System.Diagnostics.Debug.WriteLine("Found the BitmapSource for " + identifier.ToString() + " in the cache!");

         }

         return bitmapSource;

      }

 

Comments are welcomed!