C#所引用的对象类型不支持尝试的操作 [英] C# The attempted operation is not supported for the type of object referenced

查看:614
本文介绍了C#所引用的对象类型不支持尝试的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试解析字符串形式的IP地址时遇到此错误.

I got this error when I tried to parse an IP address that was in string form.

我在测试公共DNS ip地址(4.2.2.2)时(使用System.Net的IPAddress.Parse方法).

I was using a public dns ip address (4.2.2.2) while testing this (using the System.Net's IPAddress.Parse method).

它正确地进行了解析并返回了IPAddress对象.但是,如果尝试访问此对象的ScopeId属性,则会在标题中给出的消息中引发SocketException.

It does the parsing correctly and returns an IPAddress object. However, if I try to access the ScopeId property of this object, a SocketException is thrown with the message given in the title.

我真的无法弄清楚这里出了什么问题.当我检查IPAddress.ScopeId属性的文档时,它说在AddressFamily = InterNetwork时引发了异常,我的示例就是这种情况.

I really am not able to figure out whats the problem here. When I checked the documentation of IPAddress.ScopeId property, it says an exception is thrown when AddressFamily=InterNetwork which is the case with my example.

有人可以解释一下原因吗?

Could someone please explain the reason for this.

推荐答案

IPv6 特定领域.您有一个 IPv4 地址.因此,引发了异常.在这种情况下, InterNetwork 表示IPv4.

ScopeID is an IPv6 specific field. You have an IPv4 address. Therefore, an exception is raised. InterNetwork in this case means IPv4.

这篇关于C#所引用的对象类型不支持尝试的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆