'Type T = Type.GetType(TypeName); T is returning Null
Its a simple program where I am passing System.Console and it will return me the Methods it has by using Reflection ... Here Type object T is taking Null even after I am passing System.Console in txtTypeName.Text
My Code :
string TypeName = txtTypeName.Text;
Type T = Type.GetType(TypeName,true);
MethodInfo[] methods = T.GetMethods();
foreach (MethodInfo method in methods)
{
lstMethod.Items.Add(method.Name);
}
Type T = Type.GetType(TypeName); T is returning Null
Solution 1:[1]
The accepted answer is not quite correct.
The behaviour of Type.GetType did not change between framework versions. The documentation in both of them states (my bold):
Parameters
typeNameStringThe assembly-qualified name of the type to get. See
AssemblyQualifiedName. If the type is in the currently executing assembly or in mscorlib.dll/System.Private.CoreLib.dll, it is sufficient to supply the type name qualified by its namespace.
Only types that are in the calling assembly or in the BCL assembly do not need qualification. The difference between the two version of the framework is simply that System.Console got moved to a different assembly, so that is why it is failing. The docs for .NET 6 shows System.Console.dll, while NET 4.7 shows mscorlib.dll
You can, for example, see in these two fiddles what happens if you try System.DateTime without qualification: it works in both cases.
.NET 4.7
.NET 6
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Charlieface |
