views:

1806

answers:

5

I'm developing an application where I the need to invoke a method of a generic class and I don't care about the instances actual type. Something like the following Java code:

public class Item<T>{
  private T item;

  public doSomething(){...}
}

...
public void processItems(Item<?>[] items){
 for(Item<?> item : items)
   item.doSomething();
}

At the time I was on a hurry, so I solved my problem by defining a interface with the methods I needed to invoke and made the generic class implement it.

public interface IItem  
{
   void doSomething();
}

public class Item<T> : IItem {
  private T item;

  public void doSomething(){...}
}

...
public void processItems(IItem[] items)
{
 foreach(IItem item in items)
   item.doSomething();
}

This workaround works fine, but I'd like to know what is the correct way to achieve the same behavior.
Thanks.

EDIT: I forgot to refer that the caller of processItems doesn't know the actual types. Actually the idea was that the array passed as argument to processItems could contain intermixed types. Since its not possible to have such an array in .Net, using a non generic base class or interface seems to be the only way.

+1  A: 

There's no way you can omit Type Parameters in .NET generic implementation; this is by design. In fact, this can only be achieved in Java because of its type-erasure-based implementation.

You can only use a base non-generic interface (think IEnumerable<T> and IEnumerable).

Anton Gogolev
+14  A: 

The normal way to do this would be to make the method generic:

public void ProcessItems<T>(Item<T>[] items) {
  foreach(Item<T> item in items)
    item.DoSomething();
}

Assuming the caller knows the type, type inference should mean that they don't have to explicitly specify it. For example:

Item<int> items = new Item<int>(); // And then populate...
processor.ProcessItems(items);

Having said that, creating a non-generic interface specifying the type-agnostic operations can be useful as well. It will very much depend on your exact use case.

Jon Skeet
Blargh, a few seconds too soon. :P
Vilx-
Yeah, beat me too. Can you edit with an example of the caller, though?
Neil Barnwell
just `ProcessItems(data);`
Marc Gravell
Are you writing java here, Jon?
Marc Gravell
Aargh. Will fix :)
Jon Skeet
Nice except the curly brackets are in the wrong place... :p
Omar Kooheji
A: 

Further to Jon's post. making the method generic (a template) negates the requirement for this sort of functionality (using <?>). You can always feed a type into a generic class/function and for cases where you don't know what type you will need you can make the offending method/class generic as well... ultimately the user has to provide a type when calling such a function or using a generic class, for the code to be able to compile... otherwise you will get some compiler errors.

jheriko
+3  A: 

I see that you only want to invoke some method with no parameters... there's already a contract for that: Action.

public void processItems(IEnumerable<Action> actions)
{
  foreach(Action t in actions)
    t();
}

Client:

List<Animal> zoo = GetZoo();
List<Action> thingsToDo = new List<Action>();
//
thingsToDo.AddRange(zoo
  .OfType<Elephant>()
  .Select<Elephant, Action>(e => e.Trumpet));
thingsToDo.AddRange(zoo
  .OfType<Lion>()
  .Select<Lion, Action>(l => l.Roar));
thingsToDo.AddRange(zoo
  .OfType<Monkey>()
  .Select<Monkey, Action>(m => m.ThrowPoo));
//
processItems(thingsToDo);
David B
A: 

I've been struggling with the same issue when it came to porting stuff from Java where I've had constructs like

if (o instanceof Collection<?>) doSoemthing((Collection<?>)o);

Fortunately it turns out that a generic ICollection is also a non-generic ICollection and if someone needs to treat the elements in it as pure objects it is still possible:

if (o is ICollection) DoSomething((ICollection)o);

That way, since we don't care about the actual type of elements in collection all we get here are objects. A note here: if the collection was holding primitive types (int or byte for example) then autoboxing kicks in which might introduce performance penalty.

Matthias Hryniszak