Try this for size. The custom NHibernate type does not replace the type you want to open, it simply provides a mechanism for automatically matching from a stored database type to a .NET type (here, from row to color and vice versa).
public class ColorUserType : IUserType
{
public bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x == null ? typeof(Color).GetHashCode() + 473 : x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if (obj == null) return null;
var colorString = (string)obj;
return ColorTranslator.FromHtml(colorString);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
((IDataParameter)cmd.Parameters[index]).Value = ColorTranslator.ToHtml((Color)value);
}
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public SqlType[] SqlTypes
{
get { return new[] {new SqlType(DbType.StringFixedLength)}; }
}
public Type ReturnedType
{
get { return typeof(Color); }
}
public bool IsMutable
{
get { return true; }
}
}
Then the following mapping should work:
<property
name="Color"
column="hex_color"
type="YourNamespace.ColorUserType, YourAssembly" />
, FluentNHibernate, :
Map(m => m.Color).CustomTypeIs<ColorUserType>();