简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM