The normal way to do this would be to make the method generic.
Up vote 7 down vote favorite share g+ share fb share tw.
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{ 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 : 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. C# generics wildcard link|improve this question edited Feb 9 '09 at 11:53 asked Feb 9 '09 at 11:19jassuncao1,686616 94% accept rate.
The normal way to do this would be to make the method generic: public void ProcessItems(Item items) { foreach(Item 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 items = new Item(); // 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.
Blargh, a few seconds too soon. :P – Vilx- Feb 9 '09 at 11:25 Yeah, beat me too. Can you edit with an example of the caller, though?
– Neil Barnwell Feb 9 '09 at 11:27 just ProcessItems(data); – Marc Gravell? Feb 9 '09 at 11:28 1 Are you writing java here, Jon? – Marc Gravell?
Feb 9 '09 at 11:29 Aargh. Will fix :) – Jon Skeet Feb 9 '09 at 11:30.
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 actions) { foreach(Action t in actions) t(); } Client: List zoo = GetZoo(); List thingsToDo = new List(); // thingsToDo. AddRange(zoo .OfType() .
Select(e => e. Trumpet)); thingsToDo. AddRange(zoo .OfType() .
Select(l => l. Roar)); thingsToDo. AddRange(zoo .OfType() .
Select(m => m. ThrowPoo)); // processItems(thingsToDo).
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 and IEnumerable).
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.
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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.