Base data, runtime data, network data in XML
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:
public interface IRuntime { PointF Location { get; set; } double Speed { get; set; } }
Make INetwork interface:
public interface INetwork { double Speed { get; set; } double DistanceAwayFromYou { get; set; } }
Make a class that inherits from these interface:
class Car : IRuntime, INetwork { private string _model = string.Empty; private int _engineSize = 0; private PointF _location = new PointF(); private double _distanceAwayFromYou = 0; private double _runtimeSpeed = 0; private double _networkSpeed = 0; public string Model { get { return _model; } set { if (_model != value) { _model = value; } } } public int EngineSize { get { return _engineSize; } set { if (_engineSize != value) { _engineSize = value; } } } PointF IRuntime.Location { get { return _location; } set { if (_location != value) { _location = value; } } } double INetwork.DistanceAwayFromYou { get { return _distanceAwayFromYou; } set { if (_distanceAwayFromYou != value) { _distanceAwayFromYou = value; } } } double IRuntime.Speed { get { return _runtimeSpeed; } set { if (_runtimeSpeed != value) { _runtimeSpeed = value; } } } double INetwork.Speed { get { return _networkSpeed; } set { if (_networkSpeed != value) { _networkSpeed = value; } } } }
Then in your consuming code you can do as follows:
Car myCar = new Car();
//Note that you are able to set only two properties using an instance of Car i.e. Model and EngineSize
myCar.Model = "test";
myCar.EngineSize = 9;
You can save the above if you like. But if you want to save runtime data, then do as follows:
IRuntime runtimeCar = (IRuntime)myCar;
//Note that now you are able to set properties related to runtime data. This is upcasting.
runtimeCar.Location = new PointF(20, 30);
runtimeCar.Speed = 50;
For data related to network, you can do as follows:
INetwork networkCar = (INetwork)myCar;
//Now you are only able to set properties related to network data using upcast.
networkCar.Speed = 70;
networkCar.DistanceAwayFromYou = 50;
Hope this helps.
精彩评论