How to create Type from TypeCode...

Discussion in 'Microsoft Dot NET' started by Guest, Sep 21, 2005.

  1. Guest

    Guest Guest

    Does anyone know if there is a built-in function for creating a Type from a
    TypeCode?

    Type t = somefunction(TypeCode.Int16);
     
    Guest, Sep 21, 2005
    #1
    1. Advertisements

  2. Hi HairlipDog,

    Thanks for your post.

    public static object Convert.ChangeType(object, TypeCode); method has the
    similiar function, It can change an object instance into a type which
    TypeCode equals to TypeCode parameter.

    However, for your specific request, it seems that .Net Framework did not
    have build-in method. We can build a method of this function ourselves.

    If we use Reflector to view Convert.ChangeType's source code, we will see
    that it internally uses switch case statement to do the conversion. Listed
    below:
    public static object ChangeType(object value, TypeCode typeCode,
    IFormatProvider provider)
    {
    IConvertible convertible1 = value as IConvertible;
    if (convertible1 == null)
    {
    if ((value != null) || (typeCode != TypeCode.Empty))
    {
    throw new
    InvalidCastException(Environment.GetResourceString("InvalidCast_IConvertible
    "));
    }
    return null;
    }
    switch (typeCode)
    {
    case TypeCode.Empty:
    {
    throw new
    InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
    }
    case TypeCode.Object:
    {
    return value;
    }
    case TypeCode.DBNull:
    {
    throw new
    InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
    }
    case TypeCode.Boolean:
    {
    return convertible1.ToBoolean(provider);
    }
    case TypeCode.Char:
    {
    return convertible1.ToChar(provider);
    }
    case TypeCode.SByte:
    {
    return convertible1.ToSByte(provider);
    }
    case TypeCode.Byte:
    {
    return convertible1.ToByte(provider);
    }
    case TypeCode.Int16:
    {
    return convertible1.ToInt16(provider);
    }
    case TypeCode.UInt16:
    {
    return convertible1.ToUInt16(provider);
    }
    case TypeCode.Int32:
    {
    return convertible1.ToInt32(provider);
    }
    case TypeCode.UInt32:
    {
    return convertible1.ToUInt32(provider);
    }
    case TypeCode.Int64:
    {
    return convertible1.ToInt64(provider);
    }
    case TypeCode.UInt64:
    {
    return convertible1.ToUInt64(provider);
    }
    case TypeCode.Single:
    {
    return convertible1.ToSingle(provider);
    }
    case TypeCode.Double:
    {
    return convertible1.ToDouble(provider);
    }
    case TypeCode.Decimal:
    {
    return convertible1.ToDecimal(provider);
    }
    case TypeCode.DateTime:
    {
    return convertible1.ToDateTime(provider);
    }
    case TypeCode.String:
    {
    return convertible1.ToString(provider);
    }
    }
    throw new
    ArgumentException(Environment.GetResourceString("Arg_UnknownTypeCode"));
    }

    So we can also write a large switch case statement to return corresponding
    Type based on the TypeCode.

    Hope this helps.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Sep 22, 2005
    #2
    1. Advertisements

  3. Guest

    Guest Guest

    Thanks Jeffrey,

    I wound up implementing a large case statement, but wanted to check whether
    there was a built-in to ensure upward compatibility with type changes.

    -Mark
     
    Guest, Sep 22, 2005
    #3
  4. Yes, I see your concern. Currently, we have to implement a large switch
    case statement to get this done.

    If you need further help, please feel free to post. Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
     
    Jeffrey Tan[MSFT], Sep 23, 2005
    #4
  5. Guest

    Shoeless

    Joined:
    Jan 5, 2012
    Messages:
    1
    Likes Received:
    0
    Try this.

    public Type GetTypeFromTypeCode(System.TypeCode TypeCode)
    {
    switch (TypeCode) {
    case System.TypeCode.Empty:
    return null;
    default:
    return Type.GetType("System." + TypeCode.ToString);
    }
    }
     
    Shoeless, Jan 5, 2012
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. EP
    Replies:
    0
    Views:
    233
  2. Darren Gulliver
    Replies:
    1
    Views:
    365
    Darren Gulliver
    Aug 25, 2004
  3. Guest
    Replies:
    3
    Views:
    374
    Guest
    Sep 3, 2004
  4. Replies:
    1
    Views:
    493
    Mattias Sjögren
    Feb 8, 2005
  5. Guest
    Replies:
    9
    Views:
    210
    Guest
    Nov 15, 2005
Loading...

Share This Page