C# XML格式的基础数据、运行时数据、网络数据

C# XML格式的基础数据、运行时数据、网络数据,c#,xml,xsd,C#,Xml,Xsd,假设我在一个可以通过网络玩的游戏中有一个汽车类。我们的基本属性从未改变,比如模型和引擎大小,对于每一款游戏都是一样的。我们有运行时属性,如当前位置和当前速度,我们可能希望保存当前游戏并在以后重新加载。最后,我们有数据,我们必须通过网络传输给其他玩家-可能在这种情况下,位置和速度再次,但让我们说的速度和距离你 因此,我们有一个类可能看起来像(忽略确切的语法或关于标识的争论): 现在,我可以编写一个模式来为基础数据、运行时数据或网络数据创建可序列化的分部类(例如使用xsd.exe)。但在这里,我有一

假设我在一个可以通过网络玩的游戏中有一个汽车类。我们的基本属性从未改变,比如模型和引擎大小,对于每一款游戏都是一样的。我们有运行时属性,如当前位置和当前速度,我们可能希望保存当前游戏并在以后重新加载。最后,我们有数据,我们必须通过网络传输给其他玩家-可能在这种情况下,位置和速度再次,但让我们说的速度和距离你

因此,我们有一个类可能看起来像(忽略确切的语法或关于标识的争论):

现在,我可以编写一个模式来为基础数据、运行时数据或网络数据创建可序列化的分部类(例如使用xsd.exe)。但在这里,我有一个类,其中包含三个XML数据子集:

基础数据:

<Car>
    <Model>Honda</Model>
    <EngineSize>2999</EngineSize>
</Car>

本田
2999
运行时数据:

<Car>
    <Location><X>10</X><Y>20</Y></Location>
    <Speed>60.4</Speed>
</Car>

1020
60.4
网络数据:

<Car>
    <Speed>60.4</Speed>
    <DistanceFromYou>45.67</DistanceFromYou>
</Car>

60.4
45.67
我以前使用自定义属性和反射解决了这个问题;然而,它并不漂亮,而且——据我所知——不能根据模式自动验证。有没有更简单的方法,或者你会怎么做


谢谢。

如果是这种情况,请尝试以下方法:

  • 生成IRuntime接口:

    public interface IRuntime
    {       
        PointF Location
        {
            get;
            set;
        }
    
        double Speed
        {
            get;
            set;
        }
    }
    
    public interface INetwork
    {
        double Speed
        {
            get;
            set;
        }
    
        double DistanceAwayFromYou
        {
            get;
            set;
        }
    }
    
    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;
                }
            }
        }
    }
    
  • 制作iNet工作界面:

    public interface IRuntime
    {       
        PointF Location
        {
            get;
            set;
        }
    
        double Speed
        {
            get;
            set;
        }
    }
    
    public interface INetwork
    {
        double Speed
        {
            get;
            set;
        }
    
        double DistanceAwayFromYou
        {
            get;
            set;
        }
    }
    
    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;
                }
            }
        }
    }
    
  • 创建一个从以下接口继承的类:

    public interface IRuntime
    {       
        PointF Location
        {
            get;
            set;
        }
    
        double Speed
        {
            get;
            set;
        }
    }
    
    public interface INetwork
    {
        double Speed
        {
            get;
            set;
        }
    
        double DistanceAwayFromYou
        {
            get;
            set;
        }
    }
    
    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;
                }
            }
        }
    }
    
  • 然后,在消费代码中,您可以执行以下操作:

        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; 
    
        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;        
    
        INetwork networkCar = (INetwork)myCar;
        //Now you are only able to set properties related to network data using upcast.
        networkCar.Speed = 70;
        networkCar.DistanceAwayFromYou = 50;
    
    如果您愿意,可以保存以上内容。但如果要保存运行时数据,请执行以下操作:

        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; 
    
        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;        
    
        INetwork networkCar = (INetwork)myCar;
        //Now you are only able to set properties related to network data using upcast.
        networkCar.Speed = 70;
        networkCar.DistanceAwayFromYou = 50;
    
    对于与网络相关的数据,您可以执行以下操作:

        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; 
    
        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;        
    
        INetwork networkCar = (INetwork)myCar;
        //Now you are only able to set properties related to network data using upcast.
        networkCar.Speed = 70;
        networkCar.DistanceAwayFromYou = 50;
    

    希望这能有所帮助。

    我认为这不会有什么帮助,因为我需要一个运行时实例、一个单独的BaseType实例和另一个单独的NetworkType实例。然而,我确实对分部类感到好奇,但是当谈到序列化时,分部类之间在运行时可能没有区别。最后,您(正确地)不能从三个父类继承。嗯,我想知道接口?我还想使用一些自动化,比如从模式为类生成代码。@GeoffM。请查看我的最新答案,以获得在此场景中使用接口的帮助。我感谢您的尝试,但有太多我喜欢的角色。鉴于没有其他人作出回应,或许没有确切的答案。无论如何,谢谢你。