generics

Casting an object to a generic interface

I have the following interface: internal interface IRelativeTo<T> where T : IObject { T getRelativeTo(); void setRelativeTo(T relativeTo); } and a bunch of classes that (should) implement it, such as: public class AdminRateShift : IObject, IRelativeTo<AdminRateShift> { AdminRateShift getRelativeTo(); void setRelativeT...

How do I clone a generic list in C#?

I have a generic list of objects in C#, and wish to clone the list. The items within the list are cloneable, but there doesn't seem to be an option to do list.Clone() Is there an easy way around this? ...

Java generics - returning subtype of declared type from method

My class is implementing a super-class method which which returns List<JComponent>. The list being returned is read-only: public abstract class SuperClass { public abstract List<JComponent> getComponents(); } In my class, I want to return a field which is declared as List - i.e. a sub-list: public class SubClass extends SuperCla...

How do you deal with "super" generics in java?

Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private List<Animal> myList; public static void main(String args[]) { new GenericsTest(new ArrayList<Animal>()).add(new Dog()); } public GenericsTest(List<Animal> list) { myList = list; ...

How to check for the presence of an OrderBy in a ObjectQuery<T> expression tree

I'm using t4 for generating repositories for LINQ to Entities entities. The repository contains (amongst other things) a List method suitable for paging. The documentation for Supported and Unsupported Methods does not mention it, but you can't "call" Skip on a unordered IQueryable. It will raise the following exception: System.Not...

API java 5 and more: should I return an array or a Collection ?

In the spirit of Best Practices: Always return a ____, never a ____, I face a similar question in my upcoming migration from JDK1.4.2 to JDK5 and more. (Yes, I know, JDK1.4.2 is EOL! ;-) ). For functions returning a collection (which are not simple property collections), I always prefer (in JDK1.4.2) returning an Array instead of a gene...

foreach vs someList.Foreach(){}

There are apparently many ways to iterate over a collection. Curious if there are any differences, or why you'd use one way over the other. First type: List<string> someList = <some way to init> foreach(string s in someList) { <process the string> } Other Way: List<string> someList = <some way to init> someList.ForEach(delegate(s...

Best practice for a collection of generic classes

Consider the following code: abstract class SomeClassX<T> { // blah } class SomeClassY: SomeClassX<int> { // blah } class SomeClassZ: SomeClassX<long> { // blah } I want a collection of SomeClassX<T>'s, however, this isn't possible since SomeClassX<int> != SomeClassX<long> and List<SomeClassX<>> isn't allowed. So my solution ...

How do I write a Java function that returns a typed instance of 'this' and works when extended?

Hi, This is a bit of a lazyweb question but you get the rep so :-) I have a Java class that returns instances of itself to allow chaining (e.g. ClassObject.doStuff().doStuff()) For instance: public class Chainer { public Chainer doStuff() { /* Do stuff ... */ return this; } } I would like to extend this c...

Is it wrong to cast an enumerator of a child class to an enumerator of a parent class?

I've got an error in my build which says: Error 12 Cannot implicitly convert type 'System.Collections.Generic.IEnumerator< BaseClass>' to 'System.Collections.Generic.IEnumerator< IParentClass>'. An explicit conversion exists (are you missing a cast?) Is it wrong to simply cast it away? This is my code: public Dictiona...

Can C# generics have a specific base type?

Is it possible for a generic interface's type to be based on a specific parent class? For example: public interface IGenericFace<T : BaseClass> { } Obviously the above code doesn't work but if it did, what I'm trying to tell the compiler is that T must be a sub-class of BaseClass. Can that be done, are there plans for it, etc.? I t...

How do I build a Linq Expression Tree that compares against a generic object?

I have an IQueryable and an object of type T. I want to do IQueryable().Where(o => o.GetProperty(fieldName) == objectOfTypeT.GetProperty(fieldName)) so ... public IQueryable<T> DoWork<T>(string fieldName) where T : EntityObject { ... T objectOfTypeT = ...; .... return SomeIQueryable<T>().Where(o => o.GetProperty(fi...

How to use reflection to call generic Method?

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod() using the type stored in the myType variable? public class Sample { ...

Why IEnumerator of T inherts from IDisposable, but non-generic IEnumerator does NOT?

I noticed that generic IEnumerator(of T) inherits from IDisposable, but the non-generic interface IEnumerator does NOT. Why it is designed in this way? Usually, we use foreach statement to go through a IEnumerator(of T) instance. The generated code of foreach actually has try-finally block that invokes Dispose() in finally. Edit: My m...

.NET: How to check the type within a generic typed class?

Hello everybody! How do I get the type of a generic typed class within the class? An example: I build a generic typed collection implementing ICollection< T>. Within I have methods like public void Add(T item){ ... } public void Add(IEnumerable<T> enumItems){ ... } How can I ask within the method f...

Generics and Type inference

I have an abstract generic class BLL<T> where T : BusinessObject. I need to open an assembly that contains a set of concrete BLL classes, and return the tuples (businessObjectType, concreteBLLType) inside a Dictionary. There is the part of the method I could do until now, but I'm having problems to discover T. protected override Diction...

Generic contraints on derived classes

I have class A: public class ClassA<T> Class B derives from A: public class ClassB : ClassA<ClassB> Class C derives from class B: public class ClassC : ClassB Now I have a generic method with constraints public static T Method<T>() where T : ClassA<T> OK, now I want to call: ClassC c = Method<ClassC>(); but I get the compi...

Should Java raw types be deprecated?

Should a future release of Java deprecate the use of raw types to force the migration to generics? I could also see having raw types not allowed by default but allowing them via a compile flag for legacy code. ...

Is it impossible to use Generics dynamically?

I need to create at runtime instances of a class that uses generics, like class<T>, without knowing previously the type T they will have, I would like to do something like that: public Dictionary<Type, object> GenerateLists(List<Type> types) { Dictionary<Type, object> lists = new Dictionary<Type, object>(); foreach (Type type in type...

Overriding a method with Generic Parameters in Java?

I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java. The method public abstract List<? extends MonitorAccount> performMonitor(List<? extends MonitorAccount> accounts) is defined in Monitor.java and must be overridden in EmailMonitor.java. I currently have the method overridden in EmailMonitor.java a...