Type.GetType returns an instance of the System.Type class - a view on the tyoe information (metadata) about the type passed in. So it will have information like what methods and fields are in the type and what interfaces it implements.
What you want to do is coerce one type to another and so you use the (<typename>) syntax or cast which says (in the case of integral types) "give me the equivelent of this integral type as this other one - I know I may lose information as the first is bigger than the second".
Paraphrasingthe line of code that blows up in your code would produce this;
Type t = typeof(int);
myTestInt = t myLong;
this is not going to compile as "t myLong" makes no syntactic sense in C#.
Regards
Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.framework.clr/<e4**************@TK2MSFTNGP10.phx.gbl>
Hi everyone,
I'm having some trouble with the code below. I receive a compile-time
error on the second line saying "; expected":
private static void myTestFunction(long myLong)
{
System.Data.SqlTypes.SqlInt32 myTestInt;
myTestInt= (System.Type.GetType("System.Int32")) myLong;
}
However, when I put the actual type in parens, it works fine:
private static void myTestFunction(long myLong)
{
System.Data.SqlTypes.SqlInt32 myTestInt;
myTestInt= (System.Int32) myLong;
}
Would anyone know offhand what I'm doing wrong? I thought the GetType
method would do the trick here, but I'm not an expert on casting and the
GetType method.
---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (
http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004
[microsoft.public.dotnet.framework.clr]