`Assembly.GetType(name)` 何时返回 `null`? [英] When does `Assembly.GetType(name)` return `null`?

查看:32
本文介绍了`Assembly.GetType(name)` 何时返回 `null`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的代码

public static Type ToType(Type sType){组装程序集 = Assembly.Load(SerializableType.AssemblyName);type = assembly.GetType(sType.Name);}

更新

在 UI 中,如果我为基类设置值没有问题,因为他们的程序集创建正常方式,但是如果您为用户定义的类设置值,他们的程序集创建不同的方式,如下所示

解决方案

如果没有找到类名,它返回 null,很可能是因为你的 Name 属性type 只返回类型名称,而不是名称空间名称来限定它.确保您的 Name 属性也包含限定它的命名空间.

根据MSDN上的Assembly.GetType(string name),它返回:

<块引用>

代表指定类的对象,如果类为Nothing未找到.

因此,由于您获得 null,它找不到类型名称,最可能的原因是它拼写错误,或者您没有在名称空间前面添加类型名称.<块引用>

此方法仅搜索当前程序集实例.名字参数包括命名空间,但不包括程序集.

或者,也可能是类型名称的大小写错误,有一个 GetType() 版本支持不区分大小写的名称的 bool 参数也比较一下.

附言需要命名空间,因为程序集名称可能不是命名空间的指示符.也就是说,如果我在程序集 MySystem.MyClasses.DLL 中有一个类型,这并不意味着该类型一定在 MySystem.MyClasses 命名空间中.

完整的 MSDN 页面(总是很高兴看到什么东西抛出/返回)在这里:Assembly.GetType 方法(字符串)

很明显程序集存在(或者它会返回 null 并且你会得到一个 NullReferenceException),所以另一种可能性是你没有相同版本的您期望的程序集(即使用此代码的程序具有与生成数据的代码不同的程序集版本).

I have a code like shown below

public static Type ToType(Type sType)
{    
    Assembly assembly = Assembly.Load(SerializableType.AssemblyName);
    type = assembly.GetType(sType.Name);
}

UPDATE

In UI if I set value for base class no issues because their assembly creating normal way, but if you set value for user defined class their assembly creating different way like shown below

解决方案

It returns null if the class name is not found, most likely because the Name property of your type just returns the type name, and not the namespace name to qualify it. Make sure that your Name property includes the namespace qualifying it as well.

According to the MSDN on Assembly.GetType(string name), it returns:

An object that represents the specified class, or Nothing if the class is not found.

Thus since you're getting null, it couldn't find the type name, most likely reason is it's either misspelled, or you didn't prepend the type name with the namespace.

This method only searches the current assembly instance. The name parameter includes the namespace but not the assembly.

Or, it's also possible the case is wrong on the type name, there is a version of GetType() that supports a bool argument for a case-insensitive name compare as well.

p.s. The namespace is needed because the assembly name may not be an indicator of the namespace. That is, if I had a type in an assembly MySystem.MyClasses.DLL, this does not mean that the type is necessarily in the MySystem.MyClasses namespace.

The full MSDN page (always good to see what things throw/return) is here: Assembly.GetType Method(String)

It's evident the assembly exists (or it would return null and you'd get a NullReferenceException), so another possibility is you don't have the same version of the assembly you are expecting (i.e. the program using this code has a different version of the assembly then the code generating the data).

这篇关于`Assembly.GetType(name)` 何时返回 `null`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆