473,324 Members | 2,002 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,324 software developers and data experts.

Type.GetType(String) not working

In my code Type.GetType(text) works when text="System.IO.File". However, it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows.Forms.dll assembly?

Thanks
SG
Apr 27 '06 #1
9 13236
Actually you need to pass the fully qualified assembly name. The following example will reveal that more than just "System.Windows.Forms.Button" is required:

// I'm assuming this will go into a winform project, since you're referring to a button... you can call this method from the form you copy it over to.
public static void TypeTest()
{
//the actual string will be "System.Windows.Forms.Button, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" -- when you debug the code, this will become evident...
string fullyqualifiedname = new Button().GetType().AssemblyQualifiedName;
MessageBox.Show(fullyqualifiedname);
MessageBox.Show(System.Type.GetType(fullyqualified name).Name);
}

In my code Type.GetType(text) works when text="System.IO.File". However, it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows.Forms.dll assembly?

Thanks
SG
Apr 27 '06 #2
"Gugale at Lincoln" <ph*******@yahoo.com> wrote:
In my code Type.GetType(text) works when text="System.IO.File". However, it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows.Forms.dll assembly?


I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

You can however still load the button class as long as you fully qualify
the type:

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms, Culture=neutral, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089")

-- Barry
Apr 27 '06 #3
>I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.


No it's because when you don't specify the assembly name, Type.GetType
only looks in mscorlib.dll and the calling assembly. That works for
System.IO.File but not something in the Winforms assembly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 27 '06 #4
Barry Kelly <ba***********@gmail.com> wrote:
"Gugale at Lincoln" <ph*******@yahoo.com> wrote:
In my code Type.GetType(text) works when text="System.IO.File". However, it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work for
classes in System.Windows.Forms.dll assembly?


I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.


From the docs of Type.GetType(string):

<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>

(The OP is mistaken about types from System.dll - System.IO.File, for
instance, is in mscorlib.dll. Trying a type which is genuinely in
System.dll, such as System.IO.FileSystemWatcher, returns null as
expected given the above docs.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '06 #5
Mattias Sjögren <ma********************@mvps.org> wrote:
I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.


No it's because when you don't specify the assembly name, Type.GetType
only looks in mscorlib.dll and the calling assembly. That works for
System.IO.File but not something in the Winforms assembly.


At first I thought it was just what you mentioned, that the assembly name
is missing. But it's more than that.

If you try it with just the class name and assembly name and without the
Version, Culture and PublicKeyToken fields it still doesn't work. You need
all fields for it to work.

-- Barry
Apr 27 '06 #6
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
From the docs of Type.GetType(string):

<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>


If you try

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.

-- Barry
Apr 27 '06 #7
Barry Kelly <ba***********@gmail.com> wrote:
<quote>
If typeName includes only the name of the Type, this method searches in
the calling object's assembly, then in the mscorlib.dll assembly. If
typeName is fully qualified with the partial or complete assembly name,
this method searches in the specified assembly.
</quote>


If you try

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.


I don't think that counts as "enough" of the assembly name - I think
you need to give at least some of the version number. Certainly if you
give the *complete* assembly name (with full version number) it will
work. There's more to an "assembly name" than the file name. It's not
terribly well documented, unfortunately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 27 '06 #8
Thanks Barry!

In case if anyone is wondering, as I did, you can get version number and
public key using
gacutil /l assemblyname from Visual Studio .Net Command Prompt

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:4t********************************@4ax.com... "Gugale at Lincoln" <ph*******@yahoo.com> wrote:
In my code Type.GetType(text) works when text="System.IO.File". However,
it
doesn't work when text="System.Windows.Forms.Button". In general it works
for classes in System.dll assembly. Is there anyway I can make it work
for
classes in System.Windows.Forms.dll assembly?


I'm not sure why it won't work trivially for Button. Perhaps it is to do
with multiple System.Windows.Forms assemblies, one each for 1.1 and 2.0?
I'm not sure.

You can however still load the button class as long as you fully qualify
the type:

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms,
Culture=neutral, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089")

-- Barry

Apr 27 '06 #9
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Barry Kelly <ba***********@gmail.com> wrote:
If you try

| Type.GetType("System.Windows.Forms.Button, System.Windows.Forms")

you'll find it returns null, at least when you have both .NET 1.1 and 2.0
installed (I'm guessing). I tried before posting.


I don't think that counts as "enough" of the assembly name - I think
you need to give at least some of the version number. Certainly if you
give the *complete* assembly name (with full version number) it will
work. There's more to an "assembly name" than the file name. It's not
terribly well documented, unfortunately.


The version number isn't enough. You also need *both* the Culture *and*
PublicKeyToken. I tried pretty much every combination.

I've used this method myself for extensibility scenarios, and all the
times I've used it I've only needed the type name and assembly name - I've
never seen the behaviour that System.Windows.Forms.Button manifests.

Odd. <g>

-- Barry
Apr 27 '06 #10

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

Similar topics

3
by: Kendall Gifford | last post by:
Greetings. While trying to get a simple app working, I've been forced to delve into embedded and/or linked resources a bit. I read all the reference for the System.Resources namespace as well as...
3
by: the fuzz | last post by:
yeah g'day..... is it somehow possible to not include the version & public key token in the call to Type.GetType(string) ie something like Type.GetType("System.Windows.Forms.ListView,...
6
by: tshad | last post by:
The error I am getting is: ******************************************************************* Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not...
4
by: Sparky Arbuckle | last post by:
I am looping through a listbox collection to build a SQL string that will be used to delete items from a database. I have tried many variances of the code below but have had no luck. The code below...
9
by: Ben | last post by:
Hello, I'm not a developper, so sorry if it's a stupid question... I'm trying to develop an application in vb.net and I have the following problem: I have some information in an array:...
1
by: Jason Chan | last post by:
in asp.net 2.0, Page.RegisterClientScriptBlock is replaced by Client.RegisterClientScriptBlock function signature: Client.RegisterClientScriptBlock(Type, String, String) what should i put...
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}"...
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...
2
boss32178
by: boss32178 | last post by:
Ok, I am new to the form and been working with c# for 3 months now. I am creating a dll that i need to use in my asp.net page The dll grabs all the forms and creat a xml page, and grabs a xml page...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.