Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 奇怪';处置';使用';调用时出错;使用';或者直接拨打';处置';_Vb.net - Fatal编程技术网

Vb.net 奇怪';处置';使用';调用时出错;使用';或者直接拨打';处置';

Vb.net 奇怪';处置';使用';调用时出错;使用';或者直接拨打';处置';,vb.net,Vb.net,我有这样一个代码: Option Strict On Imports System Imports System.IO Imports System.ServiceModel Imports System.ServiceModel.Description ... Private Property Channel As IWSOServiceContract Public Sub SomeMethod(ByVal url As String) Using ChlFactory As

我有这样一个代码:

Option Strict On
Imports System
Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Description
...
 Private Property Channel As IWSOServiceContract

 Public Sub SomeMethod(ByVal url As String)
    Using ChlFactory As ChannelFactory(Of IWSOServiceContract) = New ChannelFactory(Of IWSOServiceContract)(New WebHttpBinding(), url)
       ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
       Channel = ChlFactory.CreateChannel()
    End Using
 End Sub
...
但当我将其重构为:

Option Strict On
Imports System
Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Description
...
Private Property ChlFactory As ChannelFactory(Of IWSOServiceContract)
Private Property Channel As IWSOServiceContract

Public Sub SomeMethod(ByVal url As String)
    ChlFactory = New ChannelFactory(Of IWSOServiceContract)(New WebHttpBinding(), url)
    ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
    Channel = ChlFactory.CreateChannel()
    ChlFactory.Dispose()   '<-- ERROR HERE BUT HOW DID USING WORK BEFORE?
End Sub 
...
选项严格打开
导入系统
导入System.IO
导入System.ServiceModel
导入System.ServiceModel.Description
...
私有财产CHLAS CHANNEL工厂(IWSO服务合同)
作为IWSOServiceContract的私有财产渠道
公共子方法(ByVal url作为字符串)
ChlFactory=新的ChannelFactory(IWSOServiceContract的)(新的WebHttpBinding(),url)
添加(新的WebHttpBehavior())
Channel=ChlFactory.CreateChannel()
ChlFactory.Dispose()“那是因为。您必须将其强制转换为
IDisposable
才能调用
Dispose


使用
语句非常聪明,可以为您进行强制转换。

错误41'Dispose'不是'System.ServiceModel.ChannelFactory(IWSOServiceContract的)'的成员。有趣的是。。。以前从没见过这样的案子。我想这就是为什么R#在我悬停在它上面时说:“这里不能访问私有处置”。那么,如果您强制转换为IDisposable,为什么私有方法会突然变为公共方法呢?这就是显式接口实现的工作原理。您实现了接口成员,但要访问它,调用方必须将您视为该接口实例,而不是具体的类。如果您希望使用另一个具有相同名称、参数集和不同类型的方法(例如,
List
上的
GetEnumerator
就是这样完成的,有三个方法,一个用于
IEnumerable
,另一个用于
IEnumerable
,还有一个不受任何接口支持)或者使用同一个成员实现多个接口,并且您希望以不同的方式实现它们。谢谢。很有启发性。以前从没见过这个案子。