开发者

convert an arraylist of different types

I have an arraylist that could contain 1 of 3 types (switch1, switch2, switch3). How do I convert this to a List?

va开发者_如何学Cr switches = _switch.Switches;  
var newList = switches.Cast<SwitchObj>().ToList();

This didn't work. Here is what is in each of switch1, switch2, switch3

string DeviceType 
string Name
string[] State


The ArrayList type is a vestigial artifact of the days before generics. You don't need to use it anymore and (if you really wanted to) you could just create a List<object>. Of course, that won't solve your problem because you are trying to cast two disparate types (string and string[]) to the same type, which doesn't make much sense.

My advice would be to simply store only one type of object in the collection. It looks like your types would all fit well as properties of a single class, so...

class Device
{
    public Device( string name, string type, string[] state = null )
    {
        Name = name;
        DeviceType = type;
        State = state;
    }

    public string Name { get; }
    public string DeviceType { get; }
    public string[] State { get; set; }
}

Now you need only populate your collection with Device objects and all of your casting issues disappear.

List<Device> devices = new List<Device>();
devices.Add( new Device( "Device1", "SomeType" ) );
devices.Add( new Device( "Device2", "SomeType" ) );
devices.Add( new Device( "Device3", "SomeType" ) );

foreach( Device d in devices )
{
    // do stuff with d.Name, d.State, and d.DeviceType
}

EDIT to address updated question:

Either the two types are compatible or they are not, there's no getting around that. Since they are not, can you modify the classes to implement a single interface? For example, if both classes implemented the following interface you could simply maintain a List<IDevice> collection and treat them generically. So, the code becomes something like...

interface IDevice
{
    string Name { get; }
    string DeviceType { get; }
    string[] State { get; set; }
}

class DeviceTypeOne : IDevice
{
    // constructor omitted

    public string Name { get; }
    public string DeviceType { get; }
    public string[] State { get; set; }
}

class DeviceTypeTwo : IDevice
{
    // constructor omitted

    public string Name { get; }
    public string DeviceType { get; }
    public string[] State { get; set; }
}

List<IDevice> devices = new List<IDevice>();
devices.Add( new DeviceTypeOne( "Device1", "SomeType" ) );
devices.Add( new DeviceTypeTwo( "Device2", "SomeType" ) );

foreach( IDevice d in devices )
{
    // do stuff with d.Name, d.State, and d.DeviceType
    // you now just deal with each object through the IDevice interface
}

If you can't modify the code then you will simply have to deal with them separately based on their type. For example:

IEnumerable<SomeDeviceType> lsOne = arrayList.OfType<SomeDeviceType>();
IEnumerable<SomeOtherDeviceType> lsTwo = arrayList.OfType<SomeOtherDeviceType>();


You can cast if all three types have the same base type. If your types are string,string and string[] there is no way other than cast to List.

What are you trying to achieve?


Instead of using Cast, you could use OfType, which will return a sequence of items that match the type specified. It's equivalent to Cast but doesn't throw if an item doesn't conform.

In your case, switches.OfType<SwitchObj>().ToList() would be a List<SwitchObj>. Any other items in the ArrayList will be ignored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜