0
比方說,我有這樣一行:爲什麼我無法獲得GetType()的MethodBody?
MethodBody getTypeMethod = typeof(object).GetMethod("GetType").GetMethodBody();
// getTypeMethod is null
爲什麼getTypeMethod
空?我如何獲得GetType()
方法的MethodBody
?
比方說,我有這樣一行:爲什麼我無法獲得GetType()的MethodBody?
MethodBody getTypeMethod = typeof(object).GetMethod("GetType").GetMethodBody();
// getTypeMethod is null
爲什麼getTypeMethod
空?我如何獲得GetType()
方法的MethodBody
?
你自己得到了答案,但只是爲了確認,Object.GetType()
是一種外部方法,外部方法沒有指定方法體。
從C#規格10.6.7
當一個方法聲明包含extern改性劑,該方法是 說是一種外部方法。外部實現外部方法 ,通常使用C#以外的語言。因爲外部方法聲明沒有提供實際的實現,所以外部方法的方法體只是由分號組成。
Here更詳細的答案。
你是否試過System.Object之外的其他東西? – StingyJack
@StingyJack Yup,嘗試與'System.String'和'System.Int32',仍然沒有運氣。 –
看起來像答案是這是一個神奇的CLR內部調用(並不太令人意外):http://stackoverflow.com/questions/5056788/object-gettype-source-code –