C# Type.GetType()返回null
我有一个web应用程序,它使用UserControl动态创建网页 在我的代码中,我有以下内容:C# Type.GetType()返回null,c#,asp.net,web-applications,user-controls,gettype,C#,Asp.net,Web Applications,User Controls,Gettype,我有一个web应用程序,它使用UserControl动态创建网页 在我的代码中,我有以下内容: private void Render_Modules() { foreach (OnlineSystemPageCustom.OnlineSystemPageHdr.OnlineSystemPageModule item in custompage.Header.Modules) { if (item.ModuleCustomO
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\u Controls.webtemplatecontrols.eventorgcommittee
用户控件的命名空间如下所示:
namespace IPAMIntranet.IPAM_Controls
我遇到的问题是该类型。GetType(typeName)返回null。这里缺少什么?仅在未在字符串中指定程序集名称时查找当前正在执行的程序集和mscorlib
选项:
- 请改用程序集限定名
- 而是调用相应的程序集
typeof(SomeKnownType).assembly
),那么第二个选项可能更简单。Type.GetType看起来像是调用程序集,还有一些系统程序集。对于其他任何内容,必须使用assemblyInstance.GetType(typeName)
,或者必须使用类型的“程序集限定名”,其中包括可以在其中找到类型的程序集详细信息。否则,将找不到它,并将返回null。您可以通过以下方式获得:
string aqn = someType.AssemblyQualifiedName;
我的问题与最初的海报非常相似,只是我需要在静态实用程序类而不是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;
}
}
公共静态类Utils
{
公共静态字符串MyFunc(字符串控制类名称)
{
字符串结果=”;
//获取此应用程序域中所有程序集的列表
Assembly[]assemblies=AppDomain.CurrentDomain.GetAssemblies();
//问题是我们不知道类在哪个程序集中定义,
//因为我们在VisualStudio中使用编译
//将它们动态地放入具有随机名称的程序集
//->但是,我们知道程序集将命名为App_Web_*
// (http://msdn.microsoft.com/en-us/magazine/cc163496.aspx)
foreach(部件中的部件)
{
if(assembly.FullName.StartsWith(“App\u Web”))
{
//我已经指定了
//相关ASCX文件中的指令,因此这应该可以工作
类型t=assembly.GetType(“ASP.+controlClassName”);
如果(t!=null)
{
//使用反射创建实例(作为常规对象)
对象o=Activator.CreateInstance(t);
//转换为具有所需属性的公共基类型
CommonBaseType ctrl=o作为CommonBaseType;
如果(ctrl!=null)
{
foreach(ctrl.PropertyWeNeed中的字符串键)
{
//最后,做实际工作
result=“好东西”;
}
}
}
}
}
返回结果;
}
}
它不美观,效率也不是很高,如果应用程序的命名约定发生变化,它很可能会崩溃(尽管您可以查看所有这些约定):但它确实有效…是的,对不起,我的键入错误,我是指nullassembly限定名?我从哪里得到的?这些是我在web应用程序本身中创建的自定义用户控件,如果helps@mattgcon:您可以使用
Type.AssemblyQualifiedName
,但如果您知道它用于特定程序集,则可以使用typeof(someClassintheasembly.assembly
获取该程序集,然后使用assembly.GetType(string)
。从程序集中使用哪个类来获取对它的引用并不重要。在我获取程序集之后,我该怎么做?我是否声明Type child=Assembly.GetType(typeName)以获取用户控件?@mattgcon:是的,完全正确。。。除了使用assembly引用而不是仅仅使用assembly
。好的,所以我添加了以下内容:“assembly aqn=typeof(webonlinecustombase)。assembly”然后“Type child=aqn.GetType(typeName)'但它仍然返回null。如何获取用户的程序集限定名controls@mattgcon我在回答中包括了这一点。如果someType是我的typeName,我必须说typeName将是动态的,我不知道在设计期间它是什么。@matt不知怎么的,你有一些字符串。我要说的是:将它们存储为程序集限定版本。或者:如果它们都来自同一个dll,请使用Assembly.GetType。问题是,这些不是我用dll添加的预编译用户控件。它们是web应用程序中的实际用户控制文件。它们位于web应用程序的子文件夹中。