Let’s say I have a Car class in a game that can be played over a network. We have basic properties that never change like model and engineSize which are the same for every game. We have runtime properties such as current location and current speed that we might want to save the current game and reload later. Finally, we have data that we must transmit to other players over the network – probably in this case location and speed again, but let’s say speed and distanceAwayFromYou.
So we have a class that might look something like (ignore exact syntax or debates over identities):
public Car
{
public string Model; /* Base data */
public int EngineSize; /* Base data */
public PointF Location; /* Runtime data */
public double Speed; /* Runtime and network data */
public double distanceAwayFromYou; /* Network data */
}
Now, I could write an schema to create serializable partial classes (using xsd.exe, for example) for base data, for runtime data, or for network data. But here I have a single class with three subsets of XML data to use:
Base data:
<Car>
<Model>Honda</Model>
<EngineSize>2999</EngineSize>
</Car>
Runtime data:
<Car>
<Location><X>10</X><Y>20</Y></Location>
<Speed>60.4</Speed>
</Car>
Network data:
<Car>
<Speed>60.4</Speed>
<DistanceFromYou>45.67</DistanceFromYou>
</Car>
I have previously solved this using custom attributes and reflection; however, it’s not pretty and – as far as I know – can’t validate automatically against a schema. Is there a simpler way, or how would you do it?
Thanks.
In that case, try this:
Make IRuntime interface:
Make INetwork interface:
Make a class that inherits from these interface:
Then in your consuming code you can do as follows:
You can save the above if you like. But if you want to save runtime data, then do as follows:
For data related to network, you can do as follows:
Hope this helps.