Type.GetType()返回null

mat*_*con 9 c# asp.net user-controls web-applications gettype

我有一个使用usercontrols动态创建网页的Web应用程序.

在我的代码中,我有以下内容:

    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);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

返回的"typeName"(示例)是:

IPAMIntranet.IPAM_Controls.webtemplatecontrols.eventorgcommittee

用户控件的命名空间如下:

namespace IPAMIntranet.IPAM_Controls
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是Type.GetType(typeName)返回null.我在这里错过了什么?

Jon*_*eet 25

Type.GetType(string)仅查找当前正在执行的程序集以及mscorlib未在字符串中指定程序集名称时.

选项:

如果您有一个简单的方法来获取相关组件(例如,通过typeof(SomeKnownType).Assembly),那么第二个选项可能更简单.

  • @mattgcon:您可以使用`Type.AssemblyQualifiedName`,但如果您知道它是针对特定程序集的,您可以使用`typeof(SomeClassInTheAssembly).Assembly`来获取该程序集,然后使用`Assembly.GetType(string)`.从程序集中使用哪个类来获取对它的引用并不重要. (2认同)