Hello,

I hope this is the right forum for this question.

I have a custom class in VB.Net.
When I instantiate it and call class.GetType() it returns what I expect ie)
AssemblyName.ClassName

However if I try :
Type.GetType("AssemblyName.ClassName") I get a TypeLoadException
"Could Not Load Type".

I have tried many different combinations of Name but It fails to load
everytime.
I feel I must be doing something wrong.
I have checked all the references and they all seem to work.
As mentioned, the class is successfully instantiated so one assumes that it
can be loaded but the Type operator does not seem to work for me.
What could I be doing wrong?

(Or, where should I post this question?)

Many thanks,

Nick

Re: Q about Type.GetType() by Scott

Scott
Fri Jul 22 10:01:47 CDT 2005

You don't use GetType on a class name, you use it on a class instance. If
you know the class name, then that's what it's type is.

GetType(StringBuilder) makes no sense, since StringBuilder is the type to
begin with. But, take the following:

Dim x As New StringBuilder
messagebox.show(x.GetType) 'Which will yeild StringBuilder




"Nick Mifsud" <nick.mifsud@systech.net> wrote in message
news:uQ8pYjrjFHA.2792@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> I hope this is the right forum for this question.
>
> I have a custom class in VB.Net.
> When I instantiate it and call class.GetType() it returns what I expect
> ie) AssemblyName.ClassName
>
> However if I try :
> Type.GetType("AssemblyName.ClassName") I get a TypeLoadException
> "Could Not Load Type".
>
> I have tried many different combinations of Name but It fails to load
> everytime.
> I feel I must be doing something wrong.
> I have checked all the references and they all seem to work.
> As mentioned, the class is successfully instantiated so one assumes that
> it can be loaded but the Type operator does not seem to work for me.
> What could I be doing wrong?
>
> (Or, where should I post this question?)
>
> Many thanks,
>
> Nick
>
>



Re: Q about Type.GetType() by larrylard

larrylard
Fri Jul 22 10:15:43 CDT 2005


Careful. What you say about GetType is true about GetType the *method*
in class Type. However, VB.NET has a built-in *operator* called GetType
which DOES take a literal class name as its argument. So:

Scott M. wrote:
> You don't use GetType on a class name, you use it on a class instance. If
> you know the class name, then that's what it's type is.

True, but you can't use String where a Type value is required - you
need to use GetType(String). This is in fact what the GetType operator
is for.

>
> GetType(StringBuilder) makes no sense, since StringBuilder is the type to
> begin with.

Type.GetType(StringBuilder) makes no sense, agreed. But
GetType(StringBuilder) is fine...




> But, take the following:
>
> Dim x As New StringBuilder
> messagebox.show(x.GetType) 'Which will yeild StringBuilder
>
>
>
>
> "Nick Mifsud" <nick.mifsud@systech.net> wrote in message
> news:uQ8pYjrjFHA.2792@TK2MSFTNGP10.phx.gbl...
> > Hello,
> >
> > I hope this is the right forum for this question.
> >
> > I have a custom class in VB.Net.
> > When I instantiate it and call class.GetType() it returns what I expect
> > ie) AssemblyName.ClassName
> >
> > However if I try :
> > Type.GetType("AssemblyName.ClassName") I get a TypeLoadException
> > "Could Not Load Type".
> >
> > I have tried many different combinations of Name but It fails to load
> > everytime.
> > I feel I must be doing something wrong.
> > I have checked all the references and they all seem to work.
> > As mentioned, the class is successfully instantiated so one assumes that
> > it can be loaded but the Type operator does not seem to work for me.
> > What could I be doing wrong?
> >
> > (Or, where should I post this question?)
> >
> > Many thanks,
> >
> > Nick
> >
> >


Re: Q about Type.GetType() by Jon

Jon
Fri Jul 22 14:02:49 CDT 2005

Scott M. <s-mar@nospam.nospam> wrote:
> You don't use GetType on a class name, you use it on a class instance. If
> you know the class name, then that's what it's type is.

No, Type.GetType(string) is a static method in the Type class.

> GetType(StringBuilder) makes no sense, since StringBuilder is the type to
> begin with.

That's not what was written though - it was the equivalent of

Type.GetType("System.Text.StringBuilder") which works fine.

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

Re: Q about Type.GetType() by Jon

Jon
Fri Jul 22 14:05:20 CDT 2005

Nick Mifsud <nick.mifsud@systech.net> wrote:
> I hope this is the right forum for this question.
>
> I have a custom class in VB.Net.
> When I instantiate it and call class.GetType() it returns what I expect ie)
> AssemblyName.ClassName

Careful - it's not AssemblyName.ClassName, but Namespace.ClassName.
They're often the same, but you need to be aware of the distinct
concepts involved.

> However if I try :
> Type.GetType("AssemblyName.ClassName") I get a TypeLoadException
> "Could Not Load Type".

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that if you're in a different assembly, you need to give the
*full* type name, including the full assembly name.
Type.GetType(string) only works with just the type name if the type is
in mscorlib or the current assembly.

However, I wouldn't expect that to give you a TypeLoadException - I'd
just expect it to return Nothing.

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

Re: Q about Type.GetType() by Lloyd

Lloyd
Sun Jul 24 05:44:32 CDT 2005

I want to add, on top everyone said, that Type.GetType() is very good at
missing type.
From what I remember of my test it would only look in the list of currently
loaded Assembly.
(which could be a small subset of all the Assembly used by your program)

"Nick Mifsud" <nick.mifsud@systech.net> wrote in message
news:uQ8pYjrjFHA.2792@TK2MSFTNGP10.phx.gbl...
> Hello,
>
> I hope this is the right forum for this question.
>
> I have a custom class in VB.Net.
> When I instantiate it and call class.GetType() it returns what I expect
> ie) AssemblyName.ClassName
>
> However if I try :
> Type.GetType("AssemblyName.ClassName") I get a TypeLoadException
> "Could Not Load Type".
>
> I have tried many different combinations of Name but It fails to load
> everytime.
> I feel I must be doing something wrong.
> I have checked all the references and they all seem to work.
> As mentioned, the class is successfully instantiated so one assumes that
> it can be loaded but the Type operator does not seem to work for me.
> What could I be doing wrong?
>
> (Or, where should I post this question?)
>
> Many thanks,
>
> Nick
>
>



Re: Q about Type.GetType() by Jon

Jon
Sun Jul 24 06:17:04 CDT 2005

Lloyd Dupont <ld@NewsAccount.galador.net> wrote:
> I want to add, on top everyone said, that Type.GetType() is very good
> at missing type. From what I remember of my test it would only look
> in the list of currently loaded Assembly. (which could be a small
> subset of all the Assembly used by your program)

I don't believe so. Try this:

using System;

public class Test
{
static void Main()
{
Console.WriteLine (Type.GetType("System.Data.DataTable, "+
"System.Data, "+
"Version=2.0.0.0, "+
"Culture=neutral, "+
"PublicKeyToken=b77a5c561934e089"));
}
}

System.Data isn't going to be loaded before Main is called, but using
Type.GetType makes the assembly get loaded.

It also works with user-defined libraries. For instance:

Lib.cs:
public class LibraryClass
{
}

Compile with: csc /target:library out:Library.dll Lib.cs

Test.cs:
using System;

public class Test
{
static void Main()
{
Console.WriteLine (Type.GetType("LibraryClass, Library"));
}
}

Compile with: csc Test.cs
(Note the lack of reference to Library.dll)

It loads the assembly and the type just fine.

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

Re: Q about Type.GetType() by Lloyd

Lloyd
Sun Jul 24 07:33:10 CDT 2005

I counter your argument with mine! (it's in the attached file).

You use fully qualified name including the assembly, that's a good idea.

However in my previous, unrigorous attempt, I just use type name (name space
+ type name)
(As you suggested, BTW in: Type.GetType("System.Text.StringBuilder"))

And this doesn't work!
tss.. you cheated ;-)

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1d4d890e12ea61fd98c503@msnews.microsoft.com...
> Lloyd Dupont <ld@NewsAccount.galador.net> wrote:
>> I want to add, on top everyone said, that Type.GetType() is very good
>> at missing type. From what I remember of my test it would only look
>> in the list of currently loaded Assembly. (which could be a small
>> subset of all the Assembly used by your program)
>
> I don't believe so. Try this:
>
> using System;
>
> public class Test
> {
> static void Main()
> {
> Console.WriteLine (Type.GetType("System.Data.DataTable, "+
> "System.Data, "+
> "Version=2.0.0.0, "+
> "Culture=neutral, "+
> "PublicKeyToken=b77a5c561934e089"));
> }
> }
>
> System.Data isn't going to be loaded before Main is called, but using
> Type.GetType makes the assembly get loaded.
>
> It also works with user-defined libraries. For instance:
>
> Lib.cs:
> public class LibraryClass
> {
> }
>
> Compile with: csc /target:library out:Library.dll Lib.cs
>
> Test.cs:
> using System;
>
> public class Test
> {
> static void Main()
> {
> Console.WriteLine (Type.GetType("LibraryClass, Library"));
> }
> }
>
> Compile with: csc Test.cs
> (Note the lack of reference to Library.dll)
>
> It loads the assembly and the type just fine.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too


begin 666 TL.cs
M=7-I;F<@4WES=&5M.PT*#0HO+R!C<V,@+VYO;&]G;R O;W5T.E1,+F5X92!4
M3"YC<R @)B8@5$P-"G!U8FQI8R!C;&%S<R!497-T#0I[#0H)<W1A=&EC('9O
M:60@36%I;B@I#0H)>PT*"0E*;VAN*"D[#0H)"4QL;WED*"D[#0H)?0T*#0H)
M<W1A=&EC('9O:60@3&QO>60H*0T*"7L-"@D)0V]N<V]L92Y7<FET94QI;F4H
M(DUY4V5L9CH@(B K(%1Y<&4N1V5T5'EP92@B5&5S="(I*3L-"@D)0V]N<V]L
M92Y7<FET94QI;F4H(D1A=&%486)L93H@(B K(%1Y<&4N1V5T5'EP92@B4WES
M=&5M+D1A=&$N1&%T851A8FQE(BDI.PT*"7T-"@T*"7-T871I8R!V;VED($IO
M:&XH*0T*"7L-"@D)0V]N<V]L92Y7<FET94QI;F4H5'EP92Y'9714>7!E*")3
M>7-T96TN1&%T82Y$871A5&%B;&4L("(@*PT*"0D)"0D)"0D)"2)3>7-T96TN
M1&%T82P@(B K#0H)"0D)"0D)"0D)(E9E<G-I;VX],BXP+C N,"P@(B K#0H)
M"0D)"0D)"0D)(D-U;'1U<F4];F5U=')A;"P@(B K#0H)"0D)"0D)"2 @(")0
K=6)L:6-+97E4;VME;CUB-S=A-6,U-C$Y,S1E,#@Y(BDI.PT*"7T-"GT-"@``
`
end


Re: Q about Type.GetType() by Jon

Jon
Sun Jul 24 09:09:18 CDT 2005

Lloyd Dupont <ld@NewsAccount.galador.net> wrote:
> I counter your argument with mine! (it's in the attached file).
>
> You use fully qualified name including the assembly, that's a good idea.

It's not just a good idea - it's required unless you're loading
something from the current assembly or mscorlib. (It has nothing to do
with whether the assembly has been loaded already or not.)

> However in my previous, unrigorous attempt, I just use type name (name space
> + type name)
> (As you suggested, BTW in: Type.GetType("System.Text.StringBuilder"))
>
> And this doesn't work!
> tss.. you cheated ;-)

No, I read the documentation, which clearly states:

<quote>
typeName can be a simple type name, a type name that includes a
namespace, or a complex name that includes an assembly name
specification.

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>

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

Re: Q about Type.GetType() by Jon

Jon
Sun Jul 24 09:10:10 CDT 2005

Lloyd Dupont <ld@NewsAccount.galador.net> wrote:
> However in my previous, unrigorous attempt, I just use type name (name space
> + type name)
> (As you suggested, BTW in: Type.GetType("System.Text.StringBuilder"))

Oops - didn't address that part. StringBuilder is in mscorlib, which is
why you don't need to specify the assembly name.

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

Re: Q about Type.GetType() by Nick

Nick
Mon Jul 25 02:24:30 CDT 2005

Hello All,

Thank you for the replies to my question.
I am slightly clearer on what I need to do!

Here is my attempt at a short but complete program:

In VB.Net
---------------------------
Imports TecBE_PickList_Item

Public Class ShortTest

Dim myBE as New TecBE_PickList_Item.clsTecBE_PickList_Item
Console.WriteLine(myBE.getType())
'Returns TecBE_PickList_Item.clsTecBE_PickList_Item

Type.GetType("TecBE_PickList_Item.clsTecBE_PickList_Item")
'TypeLoadException Occurs Here

End Class
------------------------------
Obviously TecBE_PickList_Item is an Assembly (and as pointed out has the
same name for the Namespace and the Assembly)
clsTecBE_PickList_Item is my class.
I realised that myBE.GetType() only worked when I instantiated the class.
But the fact that it does work and returns what I expect but then
Type.GetType() cannot load the required assembly despite already having
accessed it, gives me reason to believe that my name must be somehow wrong.

So, If I run this code from assembly A, and it has a project reference to
Assembly TecBE_PickList_Item, and it imports it as above, what could I be
doing wrong?

Thanks again,

Nick

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1d4b53c8dd56022698c4f0@msnews.microsoft.com...
> Nick Mifsud <nick.mifsud@systech.net> wrote:
>> I hope this is the right forum for this question.
>>
>> I have a custom class in VB.Net.
>> When I instantiate it and call class.GetType() it returns what I expect
>> ie)
>> AssemblyName.ClassName
>
> Careful - it's not AssemblyName.ClassName, but Namespace.ClassName.
> They're often the same, but you need to be aware of the distinct
> concepts involved.
>
>> However if I try :
>> Type.GetType("AssemblyName.ClassName") I get a TypeLoadException
>> "Could Not Load Type".
>
> Could you post a short but complete program which demonstrates the
> problem?
>
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
>
> Note that if you're in a different assembly, you need to give the
> *full* type name, including the full assembly name.
> Type.GetType(string) only works with just the type name if the type is
> in mscorlib or the current assembly.
>
> However, I wouldn't expect that to give you a TypeLoadException - I'd
> just expect it to return Nothing.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Re: Q about Type.GetType() by Jon

Jon
Mon Jul 25 11:53:42 CDT 2005

Nick Mifsud <nick.mifsud@systech.net> wrote:
> Thank you for the replies to my question.
> I am slightly clearer on what I need to do!
>
> Here is my attempt at a short but complete program:

Well, it's not actually complete - it doesn't compile. Never mind this
time though.

> In VB.Net
> ---------------------------
> Imports TecBE_PickList_Item
>
> Public Class ShortTest
>
> Dim myBE as New TecBE_PickList_Item.clsTecBE_PickList_Item
> Console.WriteLine(myBE.getType())
> 'Returns TecBE_PickList_Item.clsTecBE_PickList_Item
>
> Type.GetType("TecBE_PickList_Item.clsTecBE_PickList_Item")
> 'TypeLoadException Occurs Here
>
> End Class
> ------------------------------
> Obviously TecBE_PickList_Item is an Assembly (and as pointed out has the
> same name for the Namespace and the Assembly)
> clsTecBE_PickList_Item is my class.
> I realised that myBE.GetType() only worked when I instantiated the class.
> But the fact that it does work and returns what I expect but then
> Type.GetType() cannot load the required assembly despite already having
> accessed it, gives me reason to believe that my name must be somehow wrong.

No, you just haven't provided the assembly name - you've only provided
the type name. Try changing the call to GetType to:

Type.GetType("TecBE_PickList_Item.clsTecBE_PickList_Item,
tecBE_PickList_Item")

(but all on one line).

> So, If I run this code from assembly A, and it has a project reference to
> Assembly TecBE_PickList_Item, and it imports it as above, what could I be
> doing wrong?

Exactly as I've been saying all along - you're not telling it which
assembly to look in.

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

Re: Q about Type.GetType() by Nick

Nick
Tue Jul 26 02:01:45 CDT 2005

Dear Jon,

Thank you , thank you , thank you!

That now works perfectly!
None of the examples of Type.GetType usage that I had found specified the
comma and Namespace after the initial namespace.classname syntax that I was
using.

As you have explained I needed to tell it where to look.

Thanks again,

Nick
PS. Sorry the program didn't work!

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1d4f29754a180d2798c50b@msnews.microsoft.com...
> Nick Mifsud <nick.mifsud@systech.net> wrote:
>> Thank you for the replies to my question.
>> I am slightly clearer on what I need to do!
>>
>> Here is my attempt at a short but complete program:
>
> Well, it's not actually complete - it doesn't compile. Never mind this
> time though.
>
>> In VB.Net
>> ---------------------------
>> Imports TecBE_PickList_Item
>>
>> Public Class ShortTest
>>
>> Dim myBE as New TecBE_PickList_Item.clsTecBE_PickList_Item
>> Console.WriteLine(myBE.getType())
>> 'Returns TecBE_PickList_Item.clsTecBE_PickList_Item
>>
>> Type.GetType("TecBE_PickList_Item.clsTecBE_PickList_Item")
>> 'TypeLoadException Occurs Here
>>
>> End Class
>> ------------------------------
>> Obviously TecBE_PickList_Item is an Assembly (and as pointed out has the
>> same name for the Namespace and the Assembly)
>> clsTecBE_PickList_Item is my class.
>> I realised that myBE.GetType() only worked when I instantiated the class.
>> But the fact that it does work and returns what I expect but then
>> Type.GetType() cannot load the required assembly despite already having
>> accessed it, gives me reason to believe that my name must be somehow
>> wrong.
>
> No, you just haven't provided the assembly name - you've only provided
> the type name. Try changing the call to GetType to:
>
> Type.GetType("TecBE_PickList_Item.clsTecBE_PickList_Item,
> tecBE_PickList_Item")
>
> (but all on one line).
>
>> So, If I run this code from assembly A, and it has a project reference to
>> Assembly TecBE_PickList_Item, and it imports it as above, what could I be
>> doing wrong?
>
> Exactly as I've been saying all along - you're not telling it which
> assembly to look in.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too