PC Review


Reply
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average.

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
Copying a derived type to another type that inherits from derived type Bob Jones Microsoft C# .NET 3 2nd Jul 2007 02:47 PM
Copying a derived type to another type that inherits from derived type Options Bob Jones Microsoft C# .NET 1 29th Jun 2007 10:26 PM
find out if an instance of type type (indirectly) derives from another type bonk Microsoft C# .NET 8 20th Sep 2006 02:51 PM
email properties, email type, custom type or internet type? =?Utf-8?B?UmFuZHlCOTk5?= Microsoft Outlook Contacts 0 21st Jun 2005 07:01 PM
Add more type to Journal type entries under the entry type drop down list? msw Microsoft Outlook Discussion 1 7th Jan 2005 03:09 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:26 PM.