By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,188 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,188 IT Pros & Developers. It's quick & easy.

about the Type type

P: n/a
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
Nov 18 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Tony Johansson wrote:
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

--
J.
Nov 18 '08 #2

P: n/a
Tony Johansson wrote:
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
Nov 18 '08 #3

P: n/a
"Tony Johansson" <jo*****************@telia.comwrote in message
news:kl*****************@newsb.telia.net...
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:

------
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.
------

You have not supplied enough information. Try puting the namespace on the
front of the type.
Nov 18 '08 #4

P: n/a
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).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tony Johansson" <jo*****************@telia.comwrote in message
news:kl*****************@newsb.telia.net...
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

Nov 18 '08 #5

P: n/a
"Jeff Johnson" <i.***@enough.spamwrote in message
news:OG**************@TK2MSFTNGP03.phx.gbl...
>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
Okay, it just occured to me what you're trying to do. C# has a special
keyword for this:

Type test1 = typeof(name);
Nov 18 '08 #6

P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeff Johnson" <i.***@enough.spamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Jeff Johnson" <i.***@enough.spamwrote in message
news:OG**************@TK2MSFTNGP03.phx.gbl...
>>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

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

Type test1 = typeof(name);
Nov 18 '08 #7

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:5F**********************************@microsof t.com...
>Type test1 = typeof(name);
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?
Nov 18 '08 #8

P: n/a
On Tue, 18 Nov 2008 13:21:20 -0800, Tony Johansson
<jo*****************@telia.comwrote:
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
The answer is in the documentation that Jeff posted. The two methods
you're looking at have the same name, but they do entirely different
things. Thus they return entirely different values. And in the case of
Type.GetType(string), since you don't have a type named "Mytest" in your
assembly, it returns null.

Pete
Nov 18 '08 #9

P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jeff Johnson" <i.***@enough.spamwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:5F**********************************@microsof t.com...
>>Type test1 = typeof(name);
> 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?
Nov 19 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.