简体   繁体   中英

how can I get which class has been passed to method

I have a class with 9 different properties of which each one is a class

public class Vehicles
{
  Car car; //class
  Train train;  //class
  Plane plane; //class
}  

I pass this Vehicle object to a method

for example

var Vehicles = new Vehicles();
Vehicles.Car = new Car()
Object1.WorkOutTransport(vehicle)

what I need to do in Object1 is workout which 'vehicle' has been instantiated without using a switch statement and checking if the others are null or not

this is NOT a 'homework question'...I have simplified it to illustrate problem only

the actual vehicles class have 9 possible classes that could be instantiated

I would recommend rethinking your design.

Why not have all of your vehicle types implement a common interface IVehicle , then have your Vehicles class have one property named Vehicle .

You'll only have one property to worry about.

public Interface IVehicle 
{
    ... //Properties Common to all vehicles
}

public class Car : IVehicle
{
    ... //Properties to implement IVehicle
    ... //Properties specific to Car
}

public class Vehicles
{
    public IVehicle Vehicle { get; set; }
}

var vehicles = new Vehicles();
vehicles.Vehicle = new Car();
... //Do whatever else you need to do.

Assuming only one will be non-null, you can do this:

Vehicle instance = vehicle.Car ?? vehicle.Train ?? vehicle.Plane;

But if you want to do anything useful with your instance you are left having to check typeof(instance) and casting it to the right class..

You might want to consider only having one property :

public class Vehicles
{
    public Vehicle VehicleInstance {get; set;}
}

And move functionality around so that your WorkOutTransport method can act on a Vehicle instance instead of caring which subclass it has. Use virtual or abstract methods in the Vehicle class, and override them in the subclasses.

If you use different properties, not checking which is null or not can't be avoided. I suggest a base class with a property identifying the type or override the ToString method.

You could force the interface inheritors to specify their type:

enum VehicleType
{
    Passenger,
    Truck,
    // Etc...
}

public Interface IVehicle 
{
    VehicleType Type { get; }
    ... // Properties Common to all vehicles
}

public sealed class Truck : IVehicle
{
    // ... class stuff.

    // IVehicle implementation.
    public VehicleType Type { get { return VehicleType.Truck; } }
}

This will allow you to not to look over each class, but to know exactly to what type to cast.

IVehicle vehicle = GetVehicle();

switch (vehicle.Type)
    case (VehicleType.Truck)
    {
        // Do whatever you need with an instance.
        Truck truck = (Truck)vehicle;
        break;
    }
    // ... Etc

You any aother appoarch except for the switch .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM