【问题标题】:Type.GetType() returning null [duplicate]Type.GetType() 返回 null [重复]
【发布时间】:2012-02-01 17:41:59
【问题描述】:

我有一个使用用户控件动态创建网页的网络应用程序。

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

    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);
            }
        }
    }

正在返回的“typeName”(示例)是:

IPAMIntranet.IPAM_Controls.webtemplatecontrols.eventorgcommittee

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

namespace IPAMIntranet.IPAM_Controls

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

【问题讨论】:

  • 是的对不起我的打字错误,我的意思是空

标签: c# asp.net web-applications user-controls gettype


【解决方案1】:

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

选项:

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

【讨论】:

  • 程序集限定名称?我从哪里得到那个?如果有帮助,这些是我在 Web 应用程序本身中创建的自定义用户控件
  • @mattgcon:您可以使用Type.AssemblyQualifiedName,但如果您知道它用于特定程序集,您可以使用typeof(SomeClassInTheAssembly).Assembly 获取该程序集,然后使用Assembly.GetType(string)。使用程序集中的哪个类来获取对它的引用并不重要。
  • 获得程序集后,我该怎么办?我是否声明 Type child = Assembly.GetType(typeName) 以获得用户控制权?
  • @mattgcon:是的,完全是……除了使用程序集引用而不是 Assembly
  • 好的,所以我添加了以下内容:'Assembly aqn = typeof(webonlinecustombase).Assembly' 然后 'Type child = aqn.GetType(typeName)' 但它仍然返回 null
【解决方案2】:

Type.GetType 看起来像 调用 程序集和一些系统程序集。对于其他任何事情,您必须使用assemblyInstance.GetType(typeName),或者您必须使用类型的“程序集限定名称”,其中包括可以在其中找到该类型的程序集详细信息。否则,它不会被找到,并且会返回 null。你可以从:

string aqn = someType.AssemblyQualifiedName;

【讨论】:

  • 如何获取用户控件的程序集限定名称
  • @mattgcon 我将其包含在答案中
  • 如果 someType 是我的 typeName,我不得不说 typeName 将是动态的,我不知道在设计时它是什么。
  • @matt 在某处,不知何故,你有一些字符串。我要说的是:将它们存储为装配合格的版本。或者:如果它们都来自同一个 dll,请使用 Assembly.GetType
  • 问题是这些不是我用 dll 添加的预编译用户控件。它们是 Web 应用程序中的实际用户控制文件。它们位于 Web 应用程序的子文件夹中。
【解决方案3】:

我有一个与原始海报非常相似的问题,除了我需要在静态实用程序类而不是 ASPX 页面中实例化我的自定义用户控件的代码隐藏类,因此 LoadControl 对我不可用.这是我最终做的:

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;
    }
}

它不是很漂亮,效率也不是很高,如果 App_Web_* 命名约定发生变化,它可能会中断(尽管您可以查看所有这些):但它确实有效......

【讨论】:

    猜你喜欢
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2011-11-18
    • 1970-01-01
    • 2012-10-14
    相关资源
    最近更新 更多