I have a REST client that populates a Geode region with Json data which the Geode REST API automatically converts to a PdxInstance type.
The region triggers a C# native client listener AfterCreate(EntryEvent<TKey, TVal> ev)
in which the TVal
type ev.NewValue
is seen as type PdxInstanceImpl
and looks like:
PDX[7534066,__GEMFIRE_JSON]{@type=MyClass, Field1=Value1, Field2=Value2}
I've seen from here that the following code can get at the individual Pdx fields
IPdxInstance pdx = (IPdxInstance)ev.NewValue;
pdx.GetField("Field1");
and that works on a Field level, but I want to convert the PdxInstanceImpl
that is received to PdxInstance
so it can be put into another region directly, or I want to convert all the fields back to Json (as a string) in 1 go and put a Json string into another region, or use it as I like.
So there is apparently a way to autoserialize a PdxInstance
to MyClass
but if I try
MyClass c = (MyClass)pdx;
then I get System.InvalidCastException: Unable to cast object of type 'Apache.Geode.Client.Internal.PdxInstanceImpl' to type 'MyClass'
I've seen from some Java client examples you can use type PdxInstanceImpl
to get at the data but in the C# native client that gives an error: PdxInstanceImpl is inaccessible due to its protection level
.
I've added the autoserializer and the results are the same.
Any idea what I am missing here? Thanks
In the end I've used a field by field approach:
IPdxInstance pdx = (IPdxInstance)ev.NewValue;
pdx.GetField("Field1");
pdx.GetField("Field2");
pdx.GetField("Field3");
etc...
Outside of the event handlers, to create a PDX instance I used:
IPdxInstanceFactory writer = Setup.g.GetCache().CreatePdxInstanceFactory("myType");
writer.WriteString("String", "s");
writer.WriteChar("Char", 'c');
writer.WriteDouble("Double", Convert.ToDouble(1000));
IPdxInstance pdx = writer.Create();
To read a PDX instance its:
IPdxInstance pdx = Setup.gpg.GeodeGetPdx("myType", key);
MyType t = new MyType();
t.String1 = (string)pdx.GetField("String1");
t.Int1 = (int)pdx.GetField("Int1");
t.Date1 = (DateTime)pdx.GetField("Date1");
etc...