PC Review


Reply
Thread Tools Rate Thread

How to create Type from TypeCode...

 
 
=?Utf-8?B?SGFpcmxpcERvZzU4?=
Guest
Posts: n/a
 
      21st Sep 2005
Does anyone know if there is a built-in function for creating a Type from a
TypeCode?

Type t = somefunction(TypeCode.Int16);
 
Reply With Quote
 
 
 
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      22nd Sep 2005
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.

 
Reply With Quote
 
=?Utf-8?B?SGFpcmxpcERvZzU4?=
Guest
Posts: n/a
 
      22nd Sep 2005
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
 
Reply With Quote
 
Jeffrey Tan[MSFT]
Guest
Posts: n/a
 
      23rd Sep 2005
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.

 
Reply With Quote
 
New Member
Join Date: Jan 2012
Posts: 1
 
      5th Jan 2012
Try this.

public Type GetTypeFromTypeCode(System.TypeCode TypeCode)
{
switch (TypeCode) {
case System.TypeCode.Empty:
return null;
default:
return Type.GetType("System." + TypeCode.ToString);
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What TypeCode to return when implementing IConvertible GetTypeCode() Art Microsoft C# .NET 6 13th Mar 2010 10:48 AM
How to compare objects using their typecode **Developer** Microsoft VB .NET 9 28th Jun 2005 05:36 PM
Could not create type... The unProfessional Microsoft ASP .NET 1 15th Oct 2004 10:02 PM
convert DbType to/from TypeCode richlm Microsoft ADO .NET 1 15th Jun 2004 10:09 AM
Why I can not create the type? Larry Microsoft VC .NET 1 25th Sep 2003 04:05 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:34 PM.