I\'m using Dapper to hammer out some load testing tools that need to access a PostgreSQL database. This particular version of PostgreSQL does not support GUIDs natively, so
I hacked a solution together. As far as I can tell, there is no way to instruct Dapper to generate alternate binding code for a particular type so I modified the GetClassDeserializer method to force the unbox type to string if the property is a guid.
Next I re-used the code that generates a constructor call for enums.
Here's the modified code snippet (starting at line 761 of rev. rf6d62f91f31a) :
// unbox nullable enums as the primitive, i.e. byte etc
var nullUnderlyingType = Nullable.GetUnderlyingType( item.Info.Type );
var unboxType = nullUnderlyingType != null && nullUnderlyingType.IsEnum ? nullUnderlyingType : item.Info.Type;
if( unboxType == typeof(Guid))
{
unboxType = typeof (string);
}
il.Emit( OpCodes.Unbox_Any, unboxType ); // stack is now [target][target][typed-value]
if ( ( item.Info.Type == typeof( Guid ) && unboxType == typeof( string ) )
|| ( nullUnderlyingType != null && nullUnderlyingType.IsEnum ) )
{
il.Emit( OpCodes.Newobj, item.Info.Type.GetConstructor( new[] { nullUnderlyingType ?? unboxType} ) );
}
il.Emit( OpCodes.Callvirt, item.Info.Setter ); // stack is now [target]