Friday, November 28, 2008

Lazy loading class

/// Lazy loading class. When the value is required object will ask for item provider and cache result.

/// Item type.
/// Key type.
internal class LazyLoadingBase
public delegate TItem GetItemWithNameDelegate(TKey name);

GetItemWithNameDelegate getItemWithName;

TKey key;
TItem item;
bool itemSet;

public LazyLoadingBase(GetItemWithNameDelegate GetItemWithName)
itemSet = false;
item = default(TItem);
getItemWithName = GetItemWithName;

public object Value
if (!itemSet)
item = getItemWithName(key);
itemSet = true;
return item;

if (value is TItem)
item = (TItem)value;
itemSet = true;
else if (value is TKey)
key = (TKey)value;
throw new InvalidOperationException();

/// Cast operator.

public static implicit operator TItem(LazyLoadingBase lazyLoadingBaseObject)
return (TItem)lazyLoadingBaseObject.Value;

Customize debug view

I made a wrapper class for some other class. And every time when I wanted to see in debugger what does it wraps I had to view the class members and theirs members just to view that name of the wrapped class is something.

In order automatically to view important members of class and help you to fast identify object put this attribute to class:

[DebuggerDisplay("Name = {name}")]

...where "name" is class property or field name.

Wednesday, November 26, 2008

Remove tabs and spaces from end of line

If you are frustrated when you press "End" key and cursor goes much behind your last character, you can use this macro to remove all spaces and tabs from end of the line:

Sub RemoveTabsAndSpacesFromEnd()
DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
DTE.Find.FindWhat = ":b+{\n}"
DTE.Find.ReplaceWith = "\1"
DTE.Find.Target = vsFindTarget.vsFindTargetFiles
DTE.Find.MatchCase = True
DTE.Find.MatchWholeWord = False
DTE.Find.MatchInHiddenText = True
DTE.Find.SearchPath = "Current Document"
DTE.Find.SearchSubfolders = True
DTE.Find.KeepModifiedDocumentsOpen = False
DTE.Find.FilesOfType = ""
DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResults1
End Sub

Now, just assign some keyboard shortcut to this macro.

Tuesday, November 25, 2008

Exception handling block and log in the same file

If your exception handling block log exceptions in separate file, beside the fact that you set everything to use existing log category, maybe you need to set "UseDefaultLogger" to have "true" value.

Enterprice library, exception handling and logging application block

I have started to get this error message on deployment configuration:

The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, ...]) failed: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35' cannot be resolved. Please verify the spelling is correct or that the full type name is provided. (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2)

Logging works well but logging exceptions failed.

It seems that I was forgot to deploy Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll. When I put this assembly everything started to work fine.

Replace variable values

Do You know how to replace variable values without using third variable?

int x = 100;
int y = 200;

x ^= y;
y ^= x;
x ^= y;

Friday, November 21, 2008

Rhino Mock and "Type is not public, so a proxy cannot be generated"

When you try to create mock object using rhino mocks and object should implement internal interface you may get following error:
Type is not public, so a proxy cannot be generated
In order to fix it, put following line in interface assembly:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

More information at: link

Wednesday, November 05, 2008

Debug inside .net framework source code

To enable debugging inside .net framework source code follow instructions on this page:

(For symbol location enter: