0

I have 2 objects Project and License. They both inherit from the object Entity (abstract class).

Now I have an extension method "GetNewId" that contains logic to get the next id in a list of entities.

I've defined this method as an extension method, but the problem is that List (which is also a list of entities) and List don't see this method.

I guess that this problem will occur with any generic list containing objects that inherit from the same base class.

Is there a solution for this problem?

edit: working in C#

4

1 回答 1

3

What class/interface is your GetNewId() method an extension of?

If you make it an extension of IEnumerable<T> - where T is restricted to be derived from your Entity base class - you should be able to use the extension method on any collection like class containing Entity based objects:

public static GetNewId<T>(this IEnumerable<T> sequence) where T : Entity {
    // your implementation of GetNewId here
}

The Cast method (from System.Linq) is also defined on IEnumerable<T> (but on any type T, no restrictions), hence you can use it on your licenses instance.

于 2009-12-08T12:41:27.707 回答