2014-10-06 51 views
1

爲什麼var type = Type.GetType("System.Windows.Forms.TextBox");返回null如何從字符串中獲取類型?

我想從string獲得TextBox類型的類型。

+0

你就不能使用的字符串正則表達式? – nafas 2014-10-06 11:15:59

+0

@PatrickHofman我的不好,我以爲他想把班級的名字當作「String」。 – nafas 2014-10-06 11:25:46

回答

7

你應該包括完整的程序集名稱太:

var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); 

註上MSDN(重點煤礦)的文檔:

集限定名的類型來獲得的。 ...如果類型位於當前正在執行程序集的或Mscorlib.dll中,則提供通過名稱空間限定的類型名稱即可。

所以只有mscorlib和Assembly.GetExecutingAssembly()可以使用類型名稱來解析,否則你也需要全部程序集名稱。

4

你還應該包括組件名稱和公共令牌等:

var type = Type.GetType("System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"); 
相關問題