代码之家  ›  专栏  ›  技术社区  ›  user4951

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

  •  1
  • user4951  · 技术社区  · 8 年前
        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(字符串的,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 回复  |  直到 8 年前
        1
  •  1
  •   djv    8 年前

    如果接口成员已实现但标记为私有,则它不会显示在元数据或intellisense中。这是显式实现。您仍然可以通过界面访问它。这是经过设计的,因此不会增加公共界面。请记住,虽然接口成员保证可以通过接口使用,但接口并不指定访问级别。

    您需要将对象投射为 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。通过界面运行。IBaz_Run可以标记为public,但由于名称不同,它无论如何不会显示在Foo的公共界面上。

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

        2
  •  0
  •   user4951    8 年前

    我将djv答案标记为答案。

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

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

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

    所以,接口可以是私有的,我觉得这有点奇怪。