0

I'm trying to learn reflection in c#, and while learning I'm getting this exception.

'System.ArgumentNullException' occurred in mscorlib.dll error

How do I resolve this error?

class Program
{
    static void Main(string[] args)
    {
        Assembly executingAssembly = Assembly.GetExecutingAssembly();

        Type customerType = executingAssembly.GetType("Reflection.Customer");
        object customerInstance = Activator.CreateInstance(customerType);
        MethodInfo GetFullName = customerType.GetMethod("GetFullName");

        string[] methodParameter = new string[2];
        methodParameter[0] = "Minhaj";
        methodParameter[1] = "Patel";
        string Full_Name = (string)GetFullName.Invoke(customerInstance, methodParameter);
        Console.WriteLine("Full Name = {0}", Full_Name);
        Console.ReadKey();

    }
}

Customer Class code

class Customer
{
    public string GetFullName(string First_Name, string Last_Name)
    {
        return First_Name + " " + Last_Name;

    }
}

enter image description here

Minhaj Patel
  • 579
  • 1
  • 6
  • 21

2 Answers2

1

You need to check the output of GetType method, if your assembly does not have that object.

For example :

Type t = assem.GetType("Transportation.MeansOfTransportation");
      if (t != null) {

I have taken this code from https://msdn.microsoft.com/en-us/library/y0cd10tb(v=vs.110).aspx

In short, before making any call, make sure your object/input is not null.

PM.
  • 1,735
  • 1
  • 30
  • 38
  • Thank you @PM , but this only hid my `exception`, but still I'm not getting any output, as you said to check the if the object is null or not, and with the help of `GetType` method, I'm passing an object i.e. `GetType("Reflection.Customer");` – Minhaj Patel Apr 19 '17 at 06:25
  • I guess the exception is because your `GetType("Reflection.Customer");` is returning null, hence the exception. – PM. Apr 19 '17 at 23:08
0

I think u made a mistake in the below line.

Type customerType = executingAssembly.GetType("Reflection.Customer");

try printing assembly types and check what is the full name it is giving to customer class.

foreach(Type t in executingAssembly.GetTypes())
   {
      Console.WriteLine(t.FullName.ToString());
   }
Sameer549
  • 31
  • 4