Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
string strClassName = AllTypes[i].Name;
Type calledType = Type.GetType(AllTypes[i].Name);
MethodInfo[] methodInfos = calledType.GetMethods();

in calledType it give me a null value

anybody have idea about that...

Thanks in Advance,
Faizan khan
Posted
Updated 14-Mar-13 7:37am
v2
Comments
earloc 14-Mar-13 7:41am    
What Type are the elements of the AllTypes Collection?
Is it of Type "System.Type"? Then your are calling Type.GetTypes()-Method with too few information.
The "Name"-Property of the "System.Type"-Class only contains the TypeName (e.g. "String"), while the "FullName"-Property contains also information about the Namespace (e.g. "System.String")
The "AssemblyQualifiedName"-Propertty contains information about AssemlbyName, AssemblyVersion, AssemlbyCulture and PublicKeyToken
(e.g. "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
Given only the Name part of the Type, the TypeSystem won´t be able to find it, as it lacks at least the namespace-portion.
Jegan Thiyagesan 14-Mar-13 13:43pm    
What are you trying to achieve?
in your code,
you are getting the name of the type, and then the type and the collection of methods in that type, yet you haven't defined what is in the "AllTypes" collection.

Try adding namespace of the type in the string.

for example:

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


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:

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


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

Thanks,
Chinmaya
 
Share this answer
 
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:
C#
Type calledType = Type.GetType("System.String");

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900