Type.GetType() returning null [duplicate]
-
13-04-2021 - |
Pergunta
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?
Solução
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.
Outras dicas
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;
I had a very similar problem to the original poster, except that I needed to instantiate the code-behind class of my custom user-control in a static utility class rather than an ASPX page, so LoadControl wasn't available to me. Here's what I ended up doing:
public static class Utils
{
public static string MyFunc(string controlClassName)
{
string result = "";
// get a list of all assemblies in this application domain
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
// the trouble is that we don't know which assembly the class is defined in,
// because we are using the "Web Site" model in Visual Studio that compiles
// them on the fly into assemblies with random names
// -> however, we do know that the assembly will be named App_Web_*
// (http://msdn.microsoft.com/en-us/magazine/cc163496.aspx)
foreach (Assembly assembly in assemblies)
{
if (assembly.FullName.StartsWith("App_Web_"))
{
// I have specified the ClassName attribute of the <%@ Control %>
// directive in the relevant ASCX files, so this should work
Type t = assembly.GetType("ASP." + controlClassName);
if (t != null)
{
// use reflection to create the instance (as a general object)
object o = Activator.CreateInstance(t);
// cast to the common base type that has the property we need
CommonBaseType ctrl = o as CommonBaseType;
if (ctrl != null)
{
foreach (string key in ctrl.PropertyWeNeed)
{
// finally, do the actual work
result = "something good";
}
}
}
}
}
return result;
}
}
It's not pretty and not very efficient, and is liable to break if the App_Web_* naming convention changes (although you could then just look through all of them): but it does work ...