سؤال

I am using two different libraries each having their own type for a point. Both types have x and y coordinates while each has also some special fields. I would like to store both types (say PointA and PointB) in a List. I can't use base-class since PointA and PointB are library types and cannot be modified.

There is this thing that I have to actually use a List inside a List (an array of array of points). The method I call from Library1 returns List<PointA> and the library2's method returns List<PointB>.

What is the best way to store these arrays of points inside one List? Using List<List<Object>> and casting each object from the returned array to Object? Seems like this could be done more elegantly.

هل كانت مفيدة؟

المحلول

I could only think of one possible solution. Create your own "wrapper" class handling the type unification/conversion (untested):

class StoredPoint {
    PointA pa;
    PointB pb;

    public StoredPoint (PointA other) {
        pa = other;
        // pb is null
    }

    public StoredPoint (PointB other) {
        pb = other;
        // pa is null
    }

    public static implicit operator StoredPoint(PointA other) {
        return new StoredPoint(other);
    }

    public static implicit operator StoredPoint(PointB other) {
        return new StoredPoint(other);
    }

    public static implicit operator PointA(StoredPoint point) {
        if (pa != null)
            return pa;
        return PointA(0,0); // some default value in case you can't return null
    }

    public static implicit operator PointA(StoredPoint point) {
        if (pa != null)
            return pa;
        return PointA(0,0); // some default value in case you can't return null
    }

    public static implicit operator PointB(StoredPoint point) {
        if (pb != null)
            return pb;
        return PointB(0,0); // some default value in case you can't return null
    }
  }

Then you could just create a list using List<StoredPoint> and add both types of points to it. Whether you're able to use the resulting list is some different issue (mostly due to error handling etc.).

نصائح أخرى

You can just use the non-generic ArrayList, as found in the System.Collections library.

But a better option would probably be that you create your own point class and convert the PointA and PointB objects to it.

For instance, say you define your own type PointList:

public class PointList : List<MegaPoint>

(where MegaPoint is your own definition of a point).

So every item in the list is guaranteed to be of type MegaPoint. Then, if you want to add lists of the other types, implement methods such as:

public void AddFrom(List<PointA> points)

and

public void AddFrom(List<PointB> points)

which don't just add the items, but convert them to your "generic" MegaPoints.

Now your code can use the libraries as needed but your List will always contain a single type, MegaPoint, that contains the right properties for your application.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top