【问题标题】:Silverlight 4, subclassing WebClientSilverlight 4,继承 WebClient
【发布时间】:2011-11-14 05:50:22
【问题描述】:
根据建议,我在几个网页上看到(例如 Using CookieContainer with WebClient class),我将 WebClient 类子类化为使用 cookie:
public class MyWebClient : System.Net.WebClient
{
}
现在,当我初始化 MyWebClient 时:
MyWebClient wc = new MyWebClient();
它抛出 TypeLoadException。我的操作系统是 Windows 7(日文),所以错误信息不是英文的;我看到它与安全规则有关。可能是什么问题?
【问题讨论】:
标签:
silverlight
cookies
silverlight-4.0
webclient
【解决方案1】:
WebClient 的构造函数标有SecuritySafeCritical 属性。看起来这就是导致安全异常的原因。我尝试将相同的属性应用于MyWebClient 的构造函数,但没有奏效。根据我的阅读,Silverlight 中不允许这种事情。例如,请参阅this other question。
作为参考,确切的异常消息是:
System.TypeLoadException
继承安全规则被违反,而
压倒一切的成员:'MyWebClient..ctor()'。安全可访问性
覆盖方法必须与安全可访问性相匹配
方法被覆盖。
希望有更好的答案...
【解决方案2】:
您需要使用 SecuritySafeCritical 属性实现默认构造函数。今天遇到了这个问题,这就是解决方案。
public class MyWebClient : System.Net.WebClient
{
[SecuritySafeCritical]
public MyWebClient() : base() {}
}