473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

GetType returning null

Hi.

I am trying to get a type object using GetType. For this,
I use:

Type type = Type.GetType("System.Windows.Forms.TextBox");

But after this line, when I check, type is null.
What am I doing wrong ? How to do it right ?

Thank you and God bless.

_____
Marco
Nov 15 '05 #1
6 2168
Marco <a@b.c> wrote:
I am trying to get a type object using GetType. For this,
I use:

Type type = Type.GetType("System.Windows.Forms.TextBox");

But after this line, when I check, type is null.
What am I doing wrong ? How to do it right ?


Type.GetType only looks in the currently executing assembly and
mscorlib.

For the above, you should use

Type type = typeof(System.Windows.Forms.TextBox);

If you need to dynamically get another type, you should work out what
assembly it should be part of, and call Assembly.GetType appropriately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
> Type type = Type.GetType("System.Windows.Forms.TextBox");

try use typeof(TextBox) instead. this should be more reliable.
Nov 15 '05 #3
Jon and Marco,

Jon's statement:

Type.GetType only looks in the currently executing assembly and mscorlib.

Isn't completely true. If the type name that is passed in unqualified
(which is indeed the form that the original caller was using), it will
search only the current assembly and mscorlib (there is an inconsistency in
the documentation that states that it will look in System.dll and in the
current assembly). However, the following will work:

Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

So, as a general rule, when doing dynamic discovery of types, make sure
to store the fully qualified name of the type (which means full namespace,
full assembly name with version, public key, etc, etc if they exist).

However, if you know that the type that you have to get is a TextBox,
then Jon's suggestion of using typeof is the more reliable solution.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Marco <a@b.c> wrote:
I am trying to get a type object using GetType. For this,
I use:

Type type = Type.GetType("System.Windows.Forms.TextBox");

But after this line, when I check, type is null.
What am I doing wrong ? How to do it right ?


Type.GetType only looks in the currently executing assembly and
mscorlib.

For the above, you should use

Type type = typeof(System.Windows.Forms.TextBox);

If you need to dynamically get another type, you should work out what
assembly it should be part of, and call Assembly.GetType appropriately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
Jon, typeof did what I needed.
Thank you very much and God bless.

_____
Marco
Nov 15 '05 #5
Jon, typeof did what I needed.
Thank you very much and God bless.

_____
Marco
Nov 15 '05 #6
Nicholas Paldino [.NET/C# MVP] <ni**************@exisconsulting.com>
wrote:
Type.GetType only looks in the currently executing assembly and mscorlib.

Isn't completely true. If the type name that is passed in unqualified
(which is indeed the form that the original caller was using), it will
search only the current assembly and mscorlib (there is an inconsistency in
the documentation that states that it will look in System.dll and in the
current assembly). However, the following will work:
<snip>

Apologies - yes indeed. Sorry for not being clear :(
Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");

So, as a general rule, when doing dynamic discovery of types, make sure
to store the fully qualified name of the type (which means full namespace,
full assembly name with version, public key, etc, etc if they exist).


Or load the assembly separately and use Assembly.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Suresh | last post by:
Hi, I have an C# CSEXE.exe (CSexe.cs) and a CSDll.dll (CSdll.cs). exe is compiled with a reference to dll. Calling for the class Type defined in CSDLL.dll using Type.GetType(...
8
by: ron | last post by:
Hi, I am currently using the base class GetType() in the following way and was woundering if there was a different way of looking at types and doing a comparison for equality. Thanks Ron ...
3
by: Jimi | last post by:
What am I doing wrong? I'm trying to get a type object for a type which is not currently loaded in my project (otherwise I'd use "typeof"). System.Type oType =...
1
by: hazz | last post by:
"Value cannot be null.\r\nParameter name: type" is the exception thrown after the CreateInstance method below. Type t = Type.GetType(GetConfigValue("PasswordProvider")); IPasswordProvider ppdr=...
1
by: Nicolas | last post by:
I want to return the proper Null to the SQL Database if my property is nothing Also I thought about creating a function so I don't have to put so many if statement for each property for each...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
1
by: hardieca | last post by:
Hi, I have an .aspx page that needs to instantiate an object whose type in not known until runtime. The definition of said object resides in /App_code/CMS.cs CMS.cs ==================...
2
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
8
by: Martin Eckart | last post by:
Hi folks, Who can explain me why the following expression does not result in getting the correct type, but null: Type t = Type.GetType("System.Xml.XmlReader"); For "System.String" it works...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.