我想在c#中学习reflection
,并在学习过程中遇到此异常。如何解决mscorlib.dll错误中出现'System.ArgumentNullException'类型的未处理的异常
'System.ArgumentNullException' occurred in mscorlib.dll 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();
}
}
客户类代码
class Customer
{
public string GetFullName(string First_Name, string Last_Name)
{
return First_Name + " " + Last_Name;
}
}
谢谢@PM,但这只隐藏了我的'异常',但仍然没有得到任何输出,就像你说的检查对象是否为null,并且在' GetType'方法,我传递一个对象,即'GetType(“Reflection.Customer”);' –
我猜这个例外是因为你的GetType(“Reflection.Customer”);'返回null,因此是例外。 –