title: Casting to and from pdx in an Apache Geode event listener
body: <p>In <a href="https://stackoverflow.com/questions/45568028/geode-native-client-deserialise-pdxinstanceimpl">a linked question</a> I asked about a problem with Geode.Net client casting Geode PDX types to objects (domain objects) and this time I've a problem casting objects to Geode PDX types. It seems a region can hold both pdx serialized object types and other types of object and / or internal object types. By working with byte offsets I guess. </p>
  
  <p>Anyway, <a href="https://pubs.vmware.com/vfabric5/index.jsp?topic=/com.vmware.vfabric.gemfire.6.6/developing/data_serialization/program_application_for_pdx.html" rel="nofollow noreferrer">this link</a> shows a way to check for object types and handle them:</p>
  
  <pre><code>// get checks Object type and handles each appropriately
  Object myObject = myRegion.get(myKey);
  </code></pre>
  
  <p>either as PDX instances or as domain object types:</p>
  
  <pre><code>if (myObject instanceof PdxInstance) {
    // get returned PdxInstance instead of domain object    
    PdxInstance myPdxInstance = (PdxInstance)myObject;
  </code></pre>
  
  <p>then with <code>GetField</code> for a PDX Instance you can update just 1 field, very nice...</p>
  
  <p>My problem is with domain objects:</p>
  
  <pre><code>else if (myObject instanceof DomainClass) {
  // code to handle domain object instance  
  </code></pre>
  
  <p>When coming from an event listener</p>
  
  <p><code>public void AfterCreate(EntryEvent&lt;TKey, TVal&gt; ev)</code></p>
  
  <p>I first of all try to cast <code>ev.NewValue</code> to Pdx or then the domain object and keep getting one of the following:</p>
  
  <p>When the value in the region is a <code>PdxInstance</code> then without the <code>if (myObject instanceof PdxInstance)</code> as above to prevent it the cast <code>IPdxInstance pdx = (IPdxInstance)ev.NewValue</code> gives:</p>
  
  <pre><code>System.InvalidCastException: Unable to cast object of type myObject to type
  Apache.Geode.Client.IPdxInstance
  </code></pre>
  
  <p>Which is to be expected. I seem to have broken the PdxSerialisation of myObject by creating the region <code>&lt;key, value&gt;</code> as <code>&lt;string, Object&gt;</code> even though myObject extends PdxSerializable and writing keys &amp; values to the region is going through the <code>ToData()</code> override. </p>
  
  <p>So then to deal with <code>myObject</code> directly for example <code>myObject = ev.NewValue</code> or <code>myObject.Field1 = ((myObject)(ev.NewValue)).Field1</code> or like variations is giving:</p>
  
  <pre><code>Cannot implicitly convert type 'TVal' to 'myObject' 
  Cannot convert type 'TVal' to 'myObject'
  </code></pre>
  
  <p>Surely the event <code>ev.NewValue</code> to <code>myObject</code> cast ought to be straightforward so what am I missing? Otherwise I have to use <code>ev.Key</code> (which does cast without any exception) to specifically <code>get</code> the value again from the region in the cache by using:</p>
  
  <pre><code>IRegion&lt;string, Object&gt; r = cache.GetRegion&lt;string, Object&gt;(region);
  return r[key];
  </code></pre>
  
  <p>So when the object is already given in the <code>TVal</code> type <code>NewValue</code> then why I just can't access it there? <code>:-s</code></p>