开发者

Suddenly can't cast IWebProxy to WebProxy

开发者 https://www.devze.com 2023-01-14 20:12 出处:网络
I have some code that creates an IWebProxy and then casts it to a WebProxy. I ran my program and it worked. Then I inserted a break point and it all of a sudden stopped working and gives me the error:

I have some code that creates an IWebProxy and then casts it to a WebProxy. I ran my program and it worked. Then I inserted a break point and it all of a sudden stopped working and gives me the error: "Unable to cast object of type 'WebProxyWrapperOpaque' to type 'System.Net.WebProxy'."

Another thing is that we use this method in 2 other programs. I ran these programs and there was no issue with the cast.

Why did it change all of a sudden? What happened to break it? I did nothing to the code and now it's permanently broken.

How do I fix this? I have to use the IWebProxy object because it's returned from a method in code that is not my code (otherwise I'd change it), but in the end I need to use a WebProxy.

EDIT: Here's some code...

public IWebProxy ToIWebProxy(string targetAddress)
{
    //Does some proxy validation then returns an IWebProxy
}
开发者_开发知识库

somewhere else in the code I do this...

WebProxy myProxy = (WebProxy)(ToWebProxy(myAddress));

even tried this...

IWebProxy myIWebProx = ToWebProxy(myAddress);
WebProxy myProx = (WebProxy)(myIWebProx);

Again, this worked 20 minutes ago.


You cannot cast WebProxyWrapperOpaque to WebProxy. Only IWebProxy:

internal class WebProxyWrapperOpaque : IAutoWebProxy, IWebProxy
{
    // etc...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消