无法将字符串转换为类名 [英] Not able to convert string into class name

查看:73
本文介绍了无法将字符串转换为类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专业知识,



im在将字符串名称转换为clss名称时面临问题,当我将字符串转换为类型时,它给了我一个类型对象值中的空值,如下所示

Hi Expertise,

i m facing problem while converting string name into clss name,when i will convert string into Type it gives me a null values in Type object value as shown below

string strClassName = AllTypes[i].Name;
Type calledType = Type.GetType(AllTypes[i].Name);
MethodInfo[] methodInfos = calledType.GetMethods();



在calledType中它给我一个空值



任何人都有这个想法...



先谢谢,

Faizan khan


in calledType it give me a null value

anybody have idea about that...

Thanks in Advance,
Faizan khan

推荐答案

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



例如:



考虑以下类:

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



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

<的类型对象code> 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


这篇关于无法将字符串转换为类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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