2011-09-10 44 views
4

以下內容的建議,我在幾個網頁上看到(例如Using CookieContainer with WebClient class),我的子類WebClient類使用與它的cookie:Silverlight 4中,繼承WebClient的

public class MyWebClient : System.Net.WebClient 
{ 

} 

現在,當我初始化MyWebClient:

MyWebClient wc = new MyWebClient(); 

它拋出TypeLoadException。我的操作系統是Windows 7(日語),所以錯誤信息不是英文;我看到它與安全規則有關。可能是什麼問題?

+0

而不是繼承你應該使用組合 – Denis

回答

2

WebClient的構造函數標有SecuritySafeCritical屬性。看起來這是導致安全異常的原因。我嘗試將相同的屬性應用於MyWebClient的構造函數,但沒有奏效。從我讀過的文章來看,這種事情在Silverlight中是不允許的。例如,請參見this other question

作爲參考,精確異常消息爲:

System.TypeLoadException而 重寫構件違反

繼承安全規則: 'MyWebClient..ctor()'。 的安全可訪問性覆蓋方法必須匹配被覆蓋的 方法的安全性可訪問性。

我希望能有一個更好的答案...

0

您需要實現與SecuritySafeCritical屬性的默認構造函數。今天有這個問題,那就是解決方案。

public class MyWebClient : System.Net.WebClient 
{ 
    [SecuritySafeCritical] 
    public MyWebClient() : base() {} 
}