Type.GetType() returning null
Solution 1
Type.GetType(string)
only looks in the currently executing assembly and mscorlib
when you don't specify the assembly name within the string.
Options:
- Use the assembly-qualified name instead
- Call
Assembly.GetType(name)
on the appropriate assembly instead
If you have an easy way of getting hold of the relevant assembly (e.g. via typeof(SomeKnownType).Assembly
) then the second option is probably simpler.
Solution 2
Type.GetType looks as the calling assembly, and a few system assemblies. For anything else, you must either use assemblyInstance.GetType(typeName)
, or you must use the "assembly qualified name" of the type, which includes the assembly details in which the type can be found. Otherwise, it wont be found, and will return null. You can get that from:
string aqn = someType.AssemblyQualifiedName;
mattgcon
Updated on March 15, 2020Comments
-
mattgcon about 4 years
I have a web application that dynamically creates a web page using usercontrols.
Within my code I have the following:
private void Render_Modules() { foreach (OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule item in custompage.Header.Modules) { if (item.ModuleCustomOrder != 99 && !item.ModuleOptional) { string typeName = item.ModuleInternetFile; Type child = Type.GetType(typeName); webonlinecustombase ctl = (webonlinecustombase)Page.LoadControl("../IPAM_Controls/webtemplatecontrols/" + child.Name.ToString() + ".ascx"); ctl.Event = Event; ctl.custompage = custompage; ctl.custommodule = item; this.eventprogrammodules.Controls.Add(ctl); } } }
The "typeName" that is being returned (example) is:
IPAMIntranet.IPAM_Controls.webtemplatecontrols.eventorgcommittee
The namespace for the user controls is as follows:
namespace IPAMIntranet.IPAM_Controls
The problem I am having is that Type.GetType(typeName) is returning null. What am I missing here?
-
mattgcon over 12 yearsassembly-qualified name? where do I get that? These are custom user controls that I created within the web application itself if that helps
-
mattgcon over 12 yearshow do I get the assembly qualified name for the user controls
-
Marc Gravell over 12 years@mattgcon I included that in the answerr
-
Jon Skeet over 12 years@mattgcon: You can use
Type.AssemblyQualifiedName
, but if you know it's for a specific assembly, you could usetypeof(SomeClassInTheAssembly).Assembly
to get that assembly, then useAssembly.GetType(string)
. It doesn't matter which class you use from the assembly to get a reference to it. -
mattgcon over 12 yearsAfter I get the assembly then what do I do? Do I state Type child = Assembly.GetType(typeName) to get the user control?
-
mattgcon over 12 yearsif the someType is my typeName, I have to say that typeName is going to be dynamic and I have no idea what it is during design time.
-
Jon Skeet over 12 years@mattgcon: Yes, exactly... except using the assembly reference instead of just
Assembly
. -
mattgcon over 12 yearsok so I added the following: 'Assembly aqn = typeof(webonlinecustombase).Assembly' then 'Type child = aqn.GetType(typeName)' but it is still returning null
-
Jon Skeet over 12 years@mattgcon: Then it sounds like either your type name is wrong, or that type isn't in the same assembly as
webonlinecustombase
. (Is that really the name of a type? You might want to look at the .NET naming conventions.) -
mattgcon over 12 years
-
Jon Skeet over 12 years@mattgcon: Sorry, chat doesn't really work for my lifestyle. Let's stick to comments.
-
Marc Gravell over 12 years@matt somewhere, somehow, you have some strings. What I am saying is: store them as the assembly-qualified version. Or: if they all come from the same dll, use Assembly.GetType
-
mattgcon over 12 yearsOk not a problem. the type is being returned from a SQL database with a type such as: 'IPAMIntranet.IPAM_Controls.webtemplatecontrols.eventorgcommittee' the usercontrol that I created has a filename of eventorgcommittee.ascx. Both eventorgcommittee.ascx and webonlinecustombase.ascx are located within the same folder
-
mattgcon over 12 yearsThe thing is these are not precompiled usercontrols that I added with a dll. they are actual usercontrol files within the web application. They are located in a sub folder within the web application.
-
Jon Skeet over 12 years@mattgcon: I don't know the details of on-the-fly compilation in ASP.NET - I suggest you try to open one of the generated assemblies in Reflector, and see what's in it. Alternatively, try to change your design so that you don't need quite this approach...
-
mattgcon over 12 yearswhat I dont understand is, why do I need to call a different assembly if the user control files are not pre compiled from a dll and instead located as files within my web application.
-
Marc Gravell over 12 years@web just create one, and see what the assembly-qualified-name is. IIRC they follow a pattern.
-
Jon Skeet over 12 years@mattgcon: Well the types are being created on-the-fly... I'd hoped it would be compiling all of the user controls in one assembly, which would make it work... but if it only compiles them on a "just in time" basis, you'll end up with one assembly per control, which makes it all harder.
-
mattgcon over 12 yearsYou are correct in the fact that it is one assembly per control. Here is a question, how would I take that string for the type and do a typeof(class) on it?
-
Jon Skeet over 12 years@mattgcon: You can't, because the type reference basically isn't known at compile-time - because it's compiled at execution time.
-
mattgcon over 12 yearshmmm well that makes things more difficult then. and it has to be done this way.
-
Jon Skeet over 12 years@mattgcon: It's not clear exactly what you mean by that. Do you have to use non-precompiled user-controls? (If you precompiled them, they could all be in the same assembly.) Does the logic have to be in those user controls? You could have a class which basically used
typeof
for each user control you cared about, creating a mapping from string to type - but that would basically force the early compilation anyway. -
mattgcon over 12 yearsoh what I meant was that the loading of the controls has to be at runtime. The webonlinecustombase is the base usercontrol for all of the dynamically loaded controls if that helps
-
mattgcon over 12 yearsI might have just solved my issue hold on and I will return with my results
-
mattgcon over 12 yearsOk this was solved by simply putting stripping off everything except for the control name from typePath and placing it in the LoadControl statement.
-
Jon Skeet over 12 years@mattgcon: Ah, excellent - glad you sorted it in the end!
-
AKS over 8 years@skeet , using Assemly... gives error that "An object reference is required for the non-static field, method, or property 'System.Reflection.Assembly.GetType(string)'
-
Jon Skeet over 8 years@AKS: Hence the "on the appropriate assembly" part. You need to have a reference to the relevant
Assembly
first...