W3Max
W3Max

Reputation: 3406

How to constrain parameter to Built-In Types

Please see this post for code example : How to map Type with Nhibernate (and 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)

Upvotes: 0

Views: 106

Answers (1)

ChaosPandion
ChaosPandion

Reputation: 78272

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;
}

Upvotes: 4

Related Questions