Reputation: 319
I try to make an example of late binding. So that I understand better the difference between early binding and late binding. I try it like this:
using System;
using System.Reflection;
namespace EarlyBindingVersusLateBinding
{
class Program
{
static void Main(string[] args)
{
Customer cust = new Customer();
Assembly hallo = Assembly.GetExecutingAssembly();
Type CustomerType = hallo.GetType("EarlyBindingVersusLateBinding.cust");
object customerInstance = Activator.CreateInstance(CustomerType);
MethodInfo getFullMethodName = CustomerType.GetMethod("FullName");
string[] paramaters = new string[2];
paramaters[0] = "Niels";
paramaters[1] = "Ingenieur";
string fullname = (string)getFullMethodName.Invoke(customerInstance, paramaters);
Console.WriteLine(fullname);
Console.Read();
}
}
public class Customer
{
public string FullName(string firstName, string lastName)
{
return firstName + " " + lastName;
}
}
}
but I get this exception:
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
on this line:
object customerInstance = Activator.CreateInstance(CustomerType);
And I can't figure out how to fix that.
Thank you.
Upvotes: 2
Views: 510
Reputation: 172438
So, Assembly.GetType
apparently returned null
. Let's check the documentation and find out what that means:
Return Value
Type: System.Type
A Type object that represents the specified class, or null if the class is not found.
So, the class EarlyBindingVersusLateBinding.cust
could not be found. This is not surprising, since this is not a valid type in your assembly. cust
is a local variable in your Main
method. You probably wanted to write:
Type CustomerType = hallo.GetType("EarlyBindingVersusLateBinding.Customer");
Upvotes: 3