Q about Type.GetType()

  • Thread starter Thread starter Nick Mifsud
  • Start date Start date
N

Nick Mifsud

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
 
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
 
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:
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...
 
Scott M. said:
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.
 
Nick Mifsud said:
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.
 
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)
 
Lloyd Dupont said:
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.
 
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 ;-)
 
Lloyd Dupont said:
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>
 
Lloyd Dupont said:
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.
 
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
 
Nick Mifsud said:
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.
 
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!
 
Back
Top