i’m trying to create method that will display all methods that specific type has.
the code is:
public static void AllMethods(Type t)
{
var query = from x in t.GetMethods() select x;
foreach (var item in query)
Console.WriteLine(item.Name);
}
i tried another version of this:
public static void AllMethods(Type t)
{
MethodInfo[] m = t.GetMethods();
foreach (MethodInfo item in m)
Console.WriteLine(item.Name);
}
both versions compile, but when it come to pass a parameter, the NullReferenceException occurs:
static void Main(string[] args)
{
AllMethods(Type.GetType("Z")); // Z is a class name
Console.ReadLine();
}
i guess the solution is simple, but my brain now can’t figure out it)
any suggestions?
My guess is that either
Zisn’t the fully-qualified class name (you need to include the namespace) or it’s the name of a class which is neither in mscorlib nor the calling assembly. To use a class from another assembly, you need to include the assembly name as well (including version number etc if it’s strongly-named). Or useAssembly.GetType()which is simpler, if you have a reference to the assembly already, e.g. because you know another type that is in the same assembly.Assuming I’m right, you should ignore your
AllMethodsmethod entirely. Instead check this:Of course, if you know the type at compile-time, you’d be better off using
typeof.