Groups | Blog | Home
all groups > dotnet clr > december 2006 >

dotnet clr : Strange problem


Larry Smith
12/1/2006 4:45:34 PM
Hi there,

When I run the following on my app's primary thread:

System.Type.GetType("System.Windows.Forms.Form")

It works as expected. If I then launch a thread via the "BackgroundWorker"
class immediately after the above call, and invoke the above again as soon
as the thread starts, it now returns null instead. Can anyone explain this.
Thanks.

Tom Porterfield
12/1/2006 4:57:06 PM
[quoted text, click to view]

Change the call to System.Type.GetType("System.Windows.Forms.Form", true);

This change will cause it to throw an exception if it can't determine the
type. The details in the exception might give you an indication as to what
is going wrong.
--
Tom Porterfield
Larry Smith
12/1/2006 5:07:07 PM

[quoted text, click to view]

Thanks very much. It's returning "System.TypeLoadException". Docs state that
the "... common language runtime cannot find the assembly or the type within
the assembly, or cannot load the type". I see no obvious reason for this
although I do have a form going on the primary thread (why should this
matter though). Can you or anyone else comment? Thanks.

Jon Skeet [C# MVP]
12/1/2006 10:51:49 PM
[quoted text, click to view]

I would expect the above call to return null in either thread,
personally - Type.GetType(string) only looks in the currently executing
assembly and mscorlib unless the assembly details are specified.

In the real code, are you doing this with a string variable instead of
the hard-coded type name? If you only ever want to find the above type,
just use typeof(System.Windows.Forms.Form).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Larry Smith
12/1/2006 11:08:02 PM
[quoted text, click to view]

It does work on the main thread but fails on the worker thread as noted.

[quoted text, click to view]

No. I'm actually reading the type on-the-fly from a file. I only have the
fully-qualfied path name to work with as a string
("FullNamespace.ClassName"). I need to generate the assembly-qualified name
from this however. It will always be a native .NET type. Can you steer me in
the right direction. Thanks very much.

Jon Skeet [C# MVP]
12/2/2006 12:00:00 AM
[quoted text, click to view]

That sounds very odd, and I can't reproduce it. Can you post a short
but complete program which demonstrates it working in the main thread?

[quoted text, click to view]

Well, either the file needs to specify the assembly as well, or if you
have a list of assemblies to check for all types, you could just keep
Assembly references, and call Assembly.GetType on each of them until
you find a match.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
Michael Nemtsev
12/2/2006 12:00:00 AM
Hello Larry,

Have u tried to specify fully qualified name of your assembly?

BTW, have u loaded it before calling GetType?

[quoted text, click to view]
LS> Thanks very much. It's returning "System.TypeLoadException". Docs
LS> state that the "... common language runtime cannot find the assembly
LS> or the type within the assembly, or cannot load the type". I see no

---
WBR,
Michael Nemtsev [C# MVP] :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

AddThis Social Bookmark Button