Type.GetType() returning null

mattgcon picture mattgcon · Jan 3, 2012 · Viewed 20.5k times · Source

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?

Answer

Jon Skeet picture Jon Skeet · Jan 3, 2012

Type.GetType(string) only looks in the currently executing assembly and mscorlib when you don't specify the assembly name within the string.

Options:

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.