为什么我不能直接调用接口函数?

时间:2017-11-15 18:53:46

标签: vb.net

    For Each jt In json
        Dim jo1 = CType(jt, JObject)
        Dim id = jo1.Item("MarketID").ToString
        Dim basequote = dict(id)
        Dim jo2 As IDictionary(Of String, JToken) = jo1

        If Not (jo2.ContainsKey(jsonHelper.SpecialKeyForDictoToArray)) Then
            jo1.Add(jsonHelper.SpecialKeyForDictoToArray, dict(id))
            Dim b = 1
        End If
    Next

这个想法是Jobject实现IDictionary(Of String, JToken),因此我可以做

        Dim jo2 As IDictionary(Of String, JToken) = jo1

但是,我必须在这里遗漏一些东西。

如果Jobject实现了某个界面,我应该能够

        If Not (jo1.ContainsKey(jsonHelper.SpecialKeyForDictoToArray)) 

马上?

我哪里出错?

尝试这样做会产生此屏幕截图

enter image description here

我试图检查JObject的定义。它明确指出它实现了

IDictionary(Of String,JToken)

但是,我没有看到它实现像containskey这样的函数。

   '
' Summary:
'     Represents a JSON object.
<DefaultMember("Item")>
Public Class JObject
    Inherits JContainer
    Implements IDictionary(Of String, JToken), ICollection(Of KeyValuePair(Of String, JToken)), IEnumerable(Of KeyValuePair(Of String, JToken)), IEnumerable, INotifyPropertyChanged, ICustomTypeDescriptor, INotifyPropertyChanging
    '
    ' Summary:
    '     Initializes a new instance of the Newtonsoft.Json.Linq.JObject class.
    Public Sub New()
    '
    ' Summary:
    '     Initializes a new instance of the Newtonsoft.Json.Linq.JObject class from another
    '     Newtonsoft.Json.Linq.JObject object.
    '
    ' Parameters:
    '   other:
    '     A Newtonsoft.Json.Linq.JObject object to copy from.
    Public Sub New(other As JObject)
    '
    ' Summary:
    '     Initializes a new instance of the Newtonsoft.Json.Linq.JObject class with the
    '     specified content.
    '
    ' Parameters:
    '   content:
    '     The contents of the object.
    Public Sub New(ParamArray content() As Object)
    '
    ' Summary:
    '     Initializes a new instance of the Newtonsoft.Json.Linq.JObject class with the
    '     specified content.
    '
    ' Parameters:
    '   content:
    '     The contents of the object.
    Public Sub New(content As Object)

    '
    ' Summary:
    '     Gets or sets the Newtonsoft.Json.Linq.JToken with the specified property name.
    Default Public Property Item(propertyName As String) As JToken
    '
    ' Summary:
    '     Gets the Newtonsoft.Json.Linq.JToken with the specified key.
    Default Public Overrides Property Item(key As Object) As JToken
    '
    ' Summary:
    '     Gets the node type for this Newtonsoft.Json.Linq.JToken.
    Public Overrides ReadOnly Property Type As JTokenType
    '
    ' Summary:
    '     Gets the container's children tokens.
    Protected Overrides ReadOnly Property ChildrenTokens As IList(Of JToken)

    '
    ' Summary:
    '     Occurs when a property value is changing.
    Public Event PropertyChanging As PropertyChangingEventHandler
    '
    ' Summary:
    '     Occurs when a property value changes.
    Public Event PropertyChanged As PropertyChangedEventHandler

    '
    ' Summary:
    '     Adds the specified property name.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    '
    '   value:
    '     The value.
    Public Sub Add(propertyName As String, value As JToken)
    '
    ' Summary:
    '     Writes this token to a Newtonsoft.Json.JsonWriter.
    '
    ' Parameters:
    '   writer:
    '     A Newtonsoft.Json.JsonWriter into which this method will write.
    '
    '   converters:
    '     A collection of Newtonsoft.Json.JsonConverter which will be used when writing
    '     the token.
    Public Overrides Sub WriteTo(writer As JsonWriter, ParamArray converters() As JsonConverter)
    '
    ' Summary:
    '     Raises the Newtonsoft.Json.Linq.JObject.PropertyChanging event with the provided
    '     arguments.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    Protected Overridable Sub OnPropertyChanging(propertyName As String)
    '
    ' Summary:
    '     Raises the Newtonsoft.Json.Linq.JObject.PropertyChanged event with the provided
    '     arguments.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    Protected Overridable Sub OnPropertyChanged(propertyName As String)

    '
    ' Summary:
    '     Load a Newtonsoft.Json.Linq.JObject from a string that contains JSON.
    '
    ' Parameters:
    '   json:
    '     A System.String that contains JSON.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON.
    '
    ' Exceptions:
    '   T:Newtonsoft.Json.JsonReaderException:
    '     json is not valid JSON.
    Public Shared Function Parse(json As String) As JObject
    '
    ' Summary:
    '     Load a Newtonsoft.Json.Linq.JObject from a string that contains JSON.
    '
    ' Parameters:
    '   json:
    '     A System.String that contains JSON.
    '
    '   settings:
    '     The Newtonsoft.Json.Linq.JsonLoadSettings used to load the JSON. If this is null,
    '     default load settings will be used.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON.
    '
    ' Exceptions:
    '   T:Newtonsoft.Json.JsonReaderException:
    '     json is not valid JSON.
    Public Shared Function Parse(json As String, settings As JsonLoadSettings) As JObject
    '
    ' Summary:
    '     Creates a Newtonsoft.Json.Linq.JObject from an object.
    '
    ' Parameters:
    '   o:
    '     The object that will be used to create Newtonsoft.Json.Linq.JObject.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JObject with the values of the specified object.
    Public Shared Function FromObject(o As Object) As JObject
    '
    ' Summary:
    '     Creates a Newtonsoft.Json.Linq.JObject from an object.
    '
    ' Parameters:
    '   o:
    '     The object that will be used to create Newtonsoft.Json.Linq.JObject.
    '
    '   jsonSerializer:
    '     The Newtonsoft.Json.JsonSerializer that will be used to read the object.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JObject with the values of the specified object.
    Public Shared Function FromObject(o As Object, jsonSerializer As JsonSerializer) As JObject
    '
    ' Summary:
    '     Loads a Newtonsoft.Json.Linq.JObject from a Newtonsoft.Json.JsonReader.
    '
    ' Parameters:
    '   reader:
    '     A Newtonsoft.Json.JsonReader that will be read for the content of the Newtonsoft.Json.Linq.JObject.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JObject that contains the JSON that was read from the
    '     specified Newtonsoft.Json.JsonReader.
    '
    ' Exceptions:
    '   T:Newtonsoft.Json.JsonReaderException:
    '     reader is not valid JSON.
    Public Shared Function Load(reader As JsonReader) As JObject
    '
    ' Summary:
    '     Asynchronously loads a Newtonsoft.Json.Linq.JObject from a Newtonsoft.Json.JsonReader.
    '
    ' Parameters:
    '   reader:
    '     A Newtonsoft.Json.JsonReader that will be read for the content of the Newtonsoft.Json.Linq.JObject.
    '
    '   cancellationToken:
    '     The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None.
    '
    ' Returns:
    '     A System.Threading.Tasks.Task`1 that represents the asynchronous load. The System.Threading.Tasks.Task`1.Result
    '     property returns a Newtonsoft.Json.Linq.JObject that contains the JSON that was
    '     read from the specified Newtonsoft.Json.JsonReader.
    Public Shared Function LoadAsync(reader As JsonReader, Optional cancellationToken As CancellationToken = Nothing) As Task(Of JObject)
    '
    ' Summary:
    '     Asynchronously loads a Newtonsoft.Json.Linq.JObject from a Newtonsoft.Json.JsonReader.
    '
    ' Parameters:
    '   reader:
    '     A Newtonsoft.Json.JsonReader that will be read for the content of the Newtonsoft.Json.Linq.JObject.
    '
    '   settings:
    '     The Newtonsoft.Json.Linq.JsonLoadSettings used to load the JSON. If this is null,
    '     default load settings will be used.
    '
    '   cancellationToken:
    '     The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None.
    '
    ' Returns:
    '     A System.Threading.Tasks.Task`1 that represents the asynchronous load. The System.Threading.Tasks.Task`1.Result
    '     property returns a Newtonsoft.Json.Linq.JObject that contains the JSON that was
    '     read from the specified Newtonsoft.Json.JsonReader.
    <AsyncStateMachineAttribute(GetType(<LoadAsync>d__2))>
    Public Shared Function LoadAsync(reader As JsonReader, settings As JsonLoadSettings, Optional cancellationToken As CancellationToken = Nothing) As Task(Of JObject)
    '
    ' Summary:
    '     Loads a Newtonsoft.Json.Linq.JObject from a Newtonsoft.Json.JsonReader.
    '
    ' Parameters:
    '   reader:
    '     A Newtonsoft.Json.JsonReader that will be read for the content of the Newtonsoft.Json.Linq.JObject.
    '
    '   settings:
    '     The Newtonsoft.Json.Linq.JsonLoadSettings used to load the JSON. If this is null,
    '     default load settings will be used.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JObject that contains the JSON that was read from the
    '     specified Newtonsoft.Json.JsonReader.
    '
    ' Exceptions:
    '   T:Newtonsoft.Json.JsonReaderException:
    '     reader is not valid JSON.
    Public Shared Function Load(reader As JsonReader, settings As JsonLoadSettings) As JObject
    '
    ' Summary:
    '     Gets an System.Collections.Generic.IEnumerable`1 of Newtonsoft.Json.Linq.JProperty
    '     of this object's properties.
    '
    ' Returns:
    '     An System.Collections.Generic.IEnumerable`1 of Newtonsoft.Json.Linq.JProperty
    '     of this object's properties.
    Public Function Properties() As IEnumerable(Of JProperty)
    '
    ' Summary:
    '     Gets a Newtonsoft.Json.Linq.JProperty the specified name.
    '
    ' Parameters:
    '   name:
    '     The property name.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JProperty with the specified name or null.
    Public Function [Property](name As String) As JProperty
    '
    ' Summary:
    '     Tries to get the Newtonsoft.Json.Linq.JToken with the specified property name.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    '
    '   value:
    '     The value.
    '
    ' Returns:
    '     true if a value was successfully retrieved; otherwise, false.
    Public Function TryGetValue(propertyName As String, ByRef value As JToken) As Boolean
    '
    ' Summary:
    '     Removes the property with the specified name.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    '
    ' Returns:
    '     true if item was successfully removed; otherwise, false.
    Public Function Remove(propertyName As String) As Boolean
    '
    ' Summary:
    '     Gets a Newtonsoft.Json.Linq.JEnumerable`1 of Newtonsoft.Json.Linq.JToken of this
    '     object's property values.
    '
    ' Returns:
    '     A Newtonsoft.Json.Linq.JEnumerable`1 of Newtonsoft.Json.Linq.JToken of this object's
    '     property values.
    Public Function PropertyValues() As JEnumerable(Of JToken)
    '
    ' Summary:
    '     Tries to get the Newtonsoft.Json.Linq.JToken with the specified property name.
    '     The exact property name will be searched for first and if no matching property
    '     is found then the System.StringComparison will be used to match a property.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    '
    '   value:
    '     The value.
    '
    '   comparison:
    '     One of the enumeration values that specifies how the strings will be compared.
    '
    ' Returns:
    '     true if a value was successfully retrieved; otherwise, false.
    Public Function TryGetValue(propertyName As String, comparison As StringComparison, ByRef value As JToken) As Boolean
    '
    ' Summary:
    '     Gets the Newtonsoft.Json.Linq.JToken with the specified property name. The exact
    '     property name will be searched for first and if no matching property is found
    '     then the System.StringComparison will be used to match a property.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    '
    '   comparison:
    '     One of the enumeration values that specifies how the strings will be compared.
    '
    ' Returns:
    '     The Newtonsoft.Json.Linq.JToken with the specified property name.
    Public Function GetValue(propertyName As String, comparison As StringComparison) As JToken
    '
    ' Summary:
    '     Gets the Newtonsoft.Json.Linq.JToken with the specified property name.
    '
    ' Parameters:
    '   propertyName:
    '     Name of the property.
    '
    ' Returns:
    '     The Newtonsoft.Json.Linq.JToken with the specified property name.
    Public Function GetValue(propertyName As String) As JToken
    '
    ' Summary:
    '     Returns an enumerator that can be used to iterate through the collection.
    '
    ' Returns:
    '     A System.Collections.Generic.IEnumerator`1 that can be used to iterate through
    '     the collection.
    <IteratorStateMachineAttribute(GetType(<GetEnumerator>d__61))>
    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of String, JToken))
    '
    ' Summary:
    '     Writes this token to a Newtonsoft.Json.JsonWriter asynchronously.
    '
    ' Parameters:
    '   writer:
    '     A Newtonsoft.Json.JsonWriter into which this method will write.
    '
    '   cancellationToken:
    '     The token to monitor for cancellation requests.
    '
    '   converters:
    '     A collection of Newtonsoft.Json.JsonConverter which will be used when writing
    '     the token.
    '
    ' Returns:
    '     A System.Threading.Tasks.Task that represents the asynchronous write operation.
    <AsyncStateMachineAttribute(GetType(<WriteToAsync>d__0))>
    Public Overrides Function WriteToAsync(writer As JsonWriter, cancellationToken As CancellationToken, ParamArray converters() As JsonConverter) As Task
    '
    ' Summary:
    '     Returns the System.Dynamic.DynamicMetaObject responsible for binding operations
    '     performed on this object.
    '
    ' Parameters:
    '   parameter:
    '     The expression tree representation of the runtime value.
    '
    ' Returns:
    '     The System.Dynamic.DynamicMetaObject to bind this object.
    Protected Overrides Function GetMetaObject(parameter As Expression) As DynamicMetaObject
End Class

你可以在这里查看

https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm

没有containsKey的实现。为什么呢?

2 个答案:

答案 0 :(得分:1)

如果接口成员已实现但标记为“私有”,则它不会显示在元数据或智能感知中。这是明确的实施。您仍然可以通过界面访问它。这是设计的,因此它不会增加公共接口。请记住,虽然界面成员可以通过界面获得,但接口不会指定访问级别。

您需要将对象转换为IDictionary(Of String, JToken)才能访问接口成员。

CType(jo1, IDictionary(Of String, JToken))

其中任何一个都应该起作用

If Not (CType(jo1, IDictionary(Of String, JToken)).ContainsKey(jsonHelper.SpecialKeyForDictoToArray)) Then
    jo1.Add(jsonHelper.SpecialKeyForDictoToArray, dict(id))
    Dim b = 1
End If

If Not (jo2.ContainsKey(jsonHelper.SpecialKeyForDictoToArray)) Then
    jo1.Add(jsonHelper.SpecialKeyForDictoToArray, dict(id))
    Dim b = 1
End If

通常,在显式实现接口时,必须通过接口访问显式实现的成员。其中一个原因是多接口实现。在案件中

Class Foo
    Implements IBar, IBaz

    Public Sub Run() Implements IBar.Run
        Console.WriteLine("IBar")
    End Sub

    Private Sub IBaz_Run() Implements IBaz.Run
        Console.WriteLine("IBaz")
    End Sub

End Class

Interface IBar
    Sub Run()
End Interface

Interface IBaz
    Sub Run()
End Interface

Module Module1

    Sub Main()
        Dim myFoo As New Foo
        myFoo.Run() ' outputs IBar
        Dim myBaz As IBaz
        myBaz = DirectCast(myFoo, IBaz)
        myBaz.Run() ' outputs IBaz
        Console.ReadLine()
    End Sub

End Module

Foo类实现了两个具有相同成员名的接口,但只能在类Foo中定义一个接口。默认情况下,IBaz.Run()被标记为私有(显式实现)。必须通过界面访问IBaz.Run。 IBaz_Run可以被标记为公开,但由于名称不同,它无论如何都不会出现在Foo的公共界面上。

此示例描述了一种行为,但它是语言功能显式实现的副作用。

答案 1 :(得分:0)

我将djv答案标记为答案。

话虽如此,我进一步环顾四周,发现了这个

https://msdn.microsoft.com/en-us/library/28e2e18x%28VS.100%29.aspx?f=255&MSPPError=-2147217396

  

您可以使用私有成员来实现接口成员。当一个   私有成员实现接口的成员,该成员   即使不是,也可以通过界面获得   可直接在类的对象变量上使用。

所以,界面可以是私有的,我认为这有点奇怪。