我正在編寫一些代碼來動態地創建一些對象(屬於未知類型),我很好奇爲什麼Activator能夠生成類型,但Assembly不是。 下面是一個代碼示例:爲什麼Assembly和Activator對CreateInstance的行爲有所不同
class Program
{
static void Main(string[] args)
{
Type t = typeof (int);
object notAnInt = Assembly.GetExecutingAssembly().CreateInstance(t.FullName);//does not work
object actualInt = Activator.CreateInstance(t);//works
Console.WriteLine(notAnInt);
Console.WriteLine(actualInt);
}
}
我的問題是,爲什麼這兩個不同的行爲時,我會想象,他們都應該能夠創建一個int的實例?
**如何**不起作用? – SLaks