前段時間,我的一位同事寫了一段頗有疑問的代碼,最終被破壞了。這裏的簡單示例:是否可以通過反射來引用特定版本的程序集?
Assembly assembly = typeof(SmtpClient).Assembly;
Type _mailWriterType = assembly.GetType("System.Net.Mail.MailWriter"); //getting a reference to internal class
//create an instance of System.Net.Mail.MailWriter storing it into _mailWriter variable
//It's internal, but for now it still works
MethodInfo _sendMethod = typeof(MailMessage).GetMethod("Send",BindingFlags.Instance | BindingFlags.NonPublic);
//calling non-public method resulting in app crash
_sendMethod.Invoke(
message,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { _mailWriter, true },
null);
經過一番調查事實證明,我的開發機器上的確是有MailMessage.Send
方法的兩個參數:
System.Net.Mime.BaseWriter writer
Boolean sendEnvelope
Hovewer,我們的QA工程師已經安裝的VisualStudio 2012最近,和安裝了.NET 4.5。所以,當QA人嘗試運行應用程序時,他們得到了System.Reflection.TargetParameterCountException
。而且,它看起來像MailMessage.Send
在.NET 4.5得到了第三個參數:
System.Net.Mime.BaseWriter writer
Boolean sendEnvelope
Boolean allowUnicode
當然,我們要重新實現這段代碼停止使用非公共接口。 Hovewer,我們的項目時間緊迫,所以我們現在買不起。
因此,這裏有一個問題:有沒有什麼方法可以通過反射來引用程序集的舊版本(例如.NET 4.0)?
爲什麼不只是在項目上強制目標框架.NET 4.0? –
它被迫爲3.5。不幸的是,似乎通過反思的後期綁定並沒有考慮到它。 – J0HN
很好的現實世界爲什麼這種類型的代碼發臭+1。 – Jodrell