Hello,
I'm trying to use reflection to get the instance of a class in vb.net. I have a class 'A' in my web project and to test it, i create a new aspx page and try to write the following:
Dim t as Type = Type.GetType("A")
This returns "Nothing". But if i do this:
Dim inst as A = new A()
Dim t as Type = inst.GetType()
t's type is "A"
So how come i can't get the type with GetType even if the name is exactly the same? It does works for things like System.Math though so i'm probably missing something as a newbie.
From stackoverflow
-
Two things:
- You need to include the namespace of the type
- If the type isn't in mscorlib or the currently executing assembly, you need to specify the assembly name as well (including version numbers and public key information if it's a strongly-named assembly).
So for instance, to get hold of
System.Linq.Enumerable
you'd need something like:Type.GetType("System.Linq.Enumerable, System.Core, Version=4.0.0.0, " & _ "Culture=neutral, PublicKeyToken=b77a5c561934e089")
Marius S. : I guess that's where i get confused. It's a web project and i've added it as a class to that project. There is no namespace defined for that class, although i've tried to defined it and ended up with the same result. So i guess it should be in the currently executing assembly, unless i'm missing something.Richard : @Maruis: If you follow your second code to get a `Type` instance from a real object you can use `Type.FullName` to see the complete name including namespace. In ASP.NET depending on WAP or WSP the assembly may be created dynamically (compile on demand), so use `Assembly.GetExecutingAssembly()`.Marius S. : That's exactly what i've done with the Type.FullName. I tried doing Type.GetType(realobjecttype.FullName) and it still returned Nothing.Jon Skeet : @Marius: Don't use FullName... use AssemblyQualifiedName.Marius S. : I tried, but when i typed "thetype.", i didn't get AssemblyQualifiedName in the popup list so i thought it wasn't available.
0 comments:
Post a Comment