且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将字符串转换为类名

更新时间:2023-02-23 18:54:32

尝试在字符串中添加类型的命名空间。



例如:



考虑以下类:

Try adding namespace of the type in the string.

for example:

consider the following class:
namespace Custom{
    public class MyClass{
        .....
        .....
    }
}





让stType是一个字符串类型变量,它存储类名,我们需要获取实际类型使用该字符串变量名称:





and let stType is a string type variable which stores the class name and we need to get the actual type using that string variable name:

string stType = "Custom.MyClass";
Type calledType = Type.GetType(stType);





这里stType包含完全限定类型(Custom.MyClass)而不是只有类型名称(MyClass)。



谢谢,

Chinmaya



here stType contains fully qualified type (Custom.MyClass) rather than only the type name (MyClass).

Thanks,
Chinmaya






Type.GetType()获取类型指定名称,执行区分大小写的搜索。根据您的方案,您需要检查 AllTypes [i] .Name 中的值。您需要调试代码并检查 AllTypes [i] .Name 的值是否为not的类型。让我们举一个简单的例子:

Hi,

Type.GetType() gets the Type with the specified name, performing a case-sensitive search. As per your scenario, you need to check the value which is coming in AllTypes[i].Name. You need to debug your code and check whether the value of AllTypes[i].Name is a type of not. Lets take a simple example:
Type calledType = Type.GetType("System.String");



上面编写的代码将返回包含

AssemblyQualifiedName =System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089





--Amit


The above written code will return the type object which will contain
AssemblyQualifiedName = "System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"


--Amit