tags:

views:

43

answers:

1

Please see this post for code example : http://stackoverflow.com/questions/1516752/how-to-map-type-with-fluent-nhibernate

How would you constrain the parameter Type type (see the constructor in the linked example above)? I would like to throw an exception if the type is not part of this list : Built-In Types Table (C# Reference)

+3  A: 

This should work.

switch (Type.GetTypeCode(type))
{
    case TypeCode.Boolean:
    case TypeCode.Byte:
    case TypeCode.Char:
    case TypeCode.DBNull:
    case TypeCode.DateTime:
    case TypeCode.Decimal:
    case TypeCode.Double:
    case TypeCode.Empty:
    case TypeCode.Int16:
    case TypeCode.Int32:
    case TypeCode.Int64:
    case TypeCode.SByte:
    case TypeCode.Single:
    case TypeCode.String:
    case TypeCode.UInt16:
    case TypeCode.UInt32:
    case TypeCode.UInt64:
     break;
    default:
     if (type.GetType() != typeof(object))
     {
      throw new ArgumentException("invalid type.", "type");
     }
     break;
}
ChaosPandion
+1 for GetTypeCode!
bobbymcr