Type.GetType for referenced library returns undefined value

J

Jean Stax

Hi !

I created a sample library project. In my second project I reference
this library
and make the following call, which returns "undefined value":

Type myType = Type.GetType("SampleLib.MyClass");

My guess was that the function fails because my library isn't loaded
yet.
So I wrote the following:

SampleLib.MyClass ptrMyClass = new SampleLib.MyClass();
Type myType = Type.GetType("SampleLib.MyClass");

Unfortunately, this doesn't solve my problem. Type.GetType of any new
type defined in my second project
works just fine.

Any ideas ?

Thanks
 
E

Eric Wright [MSFT]

The MSDN documentation for System.Type.GetType() explains what's going on
here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemtypeclassgettypetopic1.asp

The documentation says: "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."

So, as you noted, you are able to look up types in the same assembly, but
not in a referenced assembly. To look up a type in a reference assembly,
add the referenced assembly name to the string. If your SampleLib.MyClass
class exists in a DLL called MyLibrary, then you would write this code to
look it up:

Type myType = Type.GetType("SampleLib.MyClass,MyLibrary");


--
Eric, Visual Studio Enterprise Frameworks and Tools

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


--------------------
| From: (e-mail address removed) (Jean Stax)
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Subject: Type.GetType for referenced library returns undefined value
| Date: 25 Oct 2003 12:51:24 -0700
| Organization: http://groups.google.com
| Lines: 22
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 212.150.123.82
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1067111485 3928 127.0.0.1 (25 Oct 2003
19:51:25 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: Sat, 25 Oct 2003 19:51:25 +0000 (UTC)
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!newsfeed.icl.net!newsfeed.fjserv.net!news.maxwell.syr.edu!postnews1.goo
gle.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:194051
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Hi !
|
| I created a sample library project. In my second project I reference
| this library
| and make the following call, which returns "undefined value":
|
| Type myType = Type.GetType("SampleLib.MyClass");
|
| My guess was that the function fails because my library isn't loaded
| yet.
| So I wrote the following:
|
| SampleLib.MyClass ptrMyClass = new SampleLib.MyClass();
| Type myType = Type.GetType("SampleLib.MyClass");
|
| Unfortunately, this doesn't solve my problem. Type.GetType of any new
| type defined in my second project
| works just fine.
|
| Any ideas ?
|
| Thanks
|
 

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