title: Pass additional data to JsonConverter
body: <p>I deserialize object where one of the properties is a foreign key (eg an identity value from a database table). During deserialization I would like to use a JsonConverter to retrieve the corresponding object from a collection.</p>
  
  <p>I know how to write a use custom JsonConverters. I don't know how to pass the collection to the JsonConverter, because the converter is specified at design time (like below), but the collection obviously only exists at runtime:</p>
  
  <pre><code>  &lt;JsonConverter(GetType(JSonCustomConverter))&gt;
    Public Property SomeProperty As SomePropertyClass
  </code></pre>
  
  <p>So the JSonCustomConverter's ReadJson should look this:</p>
  
  <pre><code>Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object
  If reader.Value Is Nothing Then Return False
    Dim value As String = reader.Value.ToString().Trim()
    retun MagicallyGetMyCollectionValue(value)
  End Function
  </code></pre>
  
  <p>So the silly function name MagicallyGetMyCollectionValue is just a placeholder to show you where I am stuck. I don't want to access the collection through a global variable, but I don't know how to pass the collection to the ReadJson either.</p>
  
  <p>I would be happy, if someone could point me in the right direction.</p>
  
  <hr>
  
  <p>Edit: Let me try to give a better example. </p>
  
  <p>Suppose I have the following class:</p>
  
  <pre><code>class ParentObject
    &lt;JssonConverter(GetType(JsonCustomConverter))&gt;
    Property SomeProperty As SomePropertyClass
  end class
  </code></pre>
  
  <p>I would deserialize my json data like this:</p>
  
  <pre><code> dim result = JsonConvert.DeserializeObject(jsonData, GetType(ParentObject))
  </code></pre>
  
  <p>Now assume, that the json data doesn't contain the complete representation of an instance of the SomePropertyClass, but only a key value e.g. an key as string. Suppose I have a collection like this:</p>
  
  <pre><code>dim cache as Dictionary(of string, SomePropertyClass)
  </code></pre>
  
  <p>That cache shall contain all the instances that I need. So my JSonCustomConverter should have a ReadJson Function like this:</p>
  
  <pre><code>Public Overrides Function ReadJson(reader As JsonReader, objectType As Type, existingValue As Object, serializer As JsonSerializer) As Object
  If reader.Value Is Nothing Then Return nothing
    Dim value As String = reader.Value.ToString().Trim()
    Dim cachedObject as SomePropertyClass = nothing
    if cache.TryGetValue(value, cachedObject) then return cachedObject
    retun Nothing ' or new SomePropertyClass(value)
  End Function
  </code></pre>
  
  <p>So I want the ReadJson to lookup the instance based on the key value. </p>
  
  <p>How would I pass the cache-Dictionary into the ReadJson function? I could use a singelton class that contains the cache an som getInstance-method to retrieve it, but I wouldn't want to do this. </p>