about the Type type

T

Tony Johansson

Hello!

If I have string name = "Mytest";
and
Type test1 = Type.GetType(name);
and
Type test2 = name.GetType();
then the first one will assign null to test1
but the second test2 will be given the correct type which is System.String

Now to my question why will test1 be assign null I would assume that
System.String would be assigned
because name is of that type

//Tony
 
J

Jeff Johnson

Tony Johansson said:
Hello!

If I have string name = "Mytest";
and
Type test1 = Type.GetType(name);
and
Type test2 = name.GetType();
then the first one will assign null to test1
but the second test2 will be given the correct type which is System.String

Now to my question why will test1 be assign null I would assume that
System.String would be assigned
because name is of that type

It doesn't matter what name's type is; it matters what its CONTENTS are.
You're calling the overload of GetType() that accepts a string, and
therefore what the function wants out of that string is, from MSDN:
 
N

Nicholas Paldino [.NET/C# MVP]

Tony,

Have you looked at the documentation of the overload of GetType which
takes a string? The string that you pass is the name of the class that you
want to get the type for, not the object that you want to get the type for
(for that, you just call GetType on the instance of the object).
 
J

Jeroen Mostert

Tony said:
If I have string name = "Mytest";
and
Type test1 = Type.GetType(name);
and
Type test2 = name.GetType();
then the first one will assign null to test1
but the second test2 will be given the correct type which is System.String

Now to my question why will test1 be assign null I would assume that
System.String would be assigned
because name is of that type
"Mytest" is not the name of a type, so Type.GetType() fails.

http://msdn.microsoft.com/library/w3f99sx1
 
A

Arne Vajhøj

Tony said:
If I have string name = "Mytest";
and
Type test1 = Type.GetType(name);
and
Type test2 = name.GetType();
then the first one will assign null to test1
but the second test2 will be given the correct type which is System.String

Now to my question why will test1 be assign null I would assume that
System.String would be assigned
because name is of that type

http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

Parameters

typeName
Type: System..::.String
The assembly-qualified name of the type to get. See
AssemblyQualifiedName. If the type is in the currently executing
assembly or in Mscorlib.dll, it is sufficient to supply the type name
qualified by its namespace.

Arne
 
J

Jeff Johnson

Okay, it just occured to me what you're trying to do. C# has a special
keyword for this:

Type test1 = typeof(name);
 
N

Nicholas Paldino [.NET/C# MVP]

That's not going to work. You can use typeof only when you use the
actual name. It's not an alias for the overload of the static GetType
method. That line will fail to compile.
 
J

Jeff Johnson

That's not going to work. You can use typeof only when you use the
actual name. It's not an alias for the overload of the static GetType
method. That line will fail to compile.

Ah, you're right. I use typeof() so infrequently I forgot that I usually
only use it as typeof(<EnumClassName>) and that you supply the real type
name, not a variable.

So using <variable>.GetType() is really the only way, isn't it?
 
N

Nicholas Paldino [.NET/C# MVP]

Jeff,

Well, assuming the variable is of the type that the OP wants to get,
then yes, that is. If all the OP has is the name in a string, then calling
the static GetType method is the way to go.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top