나는 늦은 바인딩의 예를 만들려고합니다. 그래서 나는 초기 바인딩과 후기 바인딩의 차이점을 더 잘 이해하고 있습니다. 나는 이런 식으로 시도 :어셈블리와 늦은 바인딩
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;
}
}
}
그러나 나는이 예외를 얻을 :
An unhandled exception of type 'System.ArgumentNullException' occurred in mscorlib.dll
Additional information: Value cannot be null.
이 줄에 :
object customerInstance = Activator.CreateInstance(CustomerType);
그리고 나는 그것을 해결하는 방법을 알아낼 수 없습니다.
감사합니다.
변수'CustomerType'이'null'인지 여부를 확인할 수 있습니까? 오류로 판단하면 가장 가능성이 높습니다. 그래서 질문은 hallo.GetType ("EarlyBindingVersusLateBinding.cust");가'null'을 반환하는 이유입니다. 대부분의 시나리오는 유형의 이름이 다르다는 것입니다. (GetType이 상대적인 네임 스페이스 경로를 허용하는지 여부도 알지 못합니다.) hallo.GetTypes()를 사용하여 어셈블리 내의 각 Type을 나열하여 더 자세히 알아볼 수 있습니다. – MrPaulch
코드를 디버깅하는 데 약간의 시간을 할애하는 것이 매우 분명합니다. Assembly.GetType()은 잘못된 문자열을 전달한 경우 * null *을 반환합니다. Kaboom이 다음입니다. "고객"대신 "고객"을 고려하십시오. "Cust"를 입력하게 만든 이유는 아마도 실제 질문 일뿐입니다. 추측하기 어렵습니다. –