私は後期結合の例を作りようとします。私は早期拘束と後期拘束の違いをよく理解しています。私はこのようにそれを試してみてください。アセンブリとの遅い結合
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()'を使用してアセンブリ内の各タイプをリストすることができます。 – MrPaulch
コードをデバッグするのに少し時間を費やすと非常に明白になるでしょう。 Assembly.GetType()は、間違った文字列を渡すと* null *を返します。 Kaboomは次です。 「顧客」の代わりに「顧客」を検討してください。あなたが "Cust"とタイプしたのはおそらく本当の疑問で、推測するのは難しいでしょう。 –