generics

Java generics - retrieve type

public Interface Foo<T extends Colors>{...} Is there a way to retrieve which T was given for an implementation of Foo? For example, public Class FooImpl implements Foo<Green>{..} Would return Green. ...

[Java] Generics and "where" clause

In. NET, I can do something like this: public static T[] CreateAndFillArray<T>(int size) where T : new() { T[] array = new T[size]; for (int i = size - 1; i >= 0; i--) array[i] = new T(); return array; } We must to specify "where T : new()" clause. How to do it in Java? ...

How can I restrict the children of nodes in a tree structure.

I'm creating a tree structure that is based on an AbstractNode class. The AbstractNode class has a generic collection property that contain its child nodes. See the code example below. Is there some way, possibly using generics, that I can restrict a concrete version of AbstractNode to only allow one type of child node? See the code bel...

Using specific types based on typeof inside generic function

I want to have a generic function for a few types such as long, TimeSpan and DateTime. public static T Parse<T>(string text) { T store; if(typeof(T) == typeof(TimeSpan) store = (T)((object) new TimeSpan(0, 1, 0)); else { T.tryParse(text, out store); } return store; } Is there a better way than ...

How can I split a generic List (of T) based on a property of a list member?

I have a generic List (of Foo) which contains n objects of Type Foo. One of the properties of Foo is PropertyA. PropertyA can be one of ValueA, ValueB or ValueC. Is there an easy way of splitting this into three seperate Lists, one for ValueA, one for ValueB and one for ValueC? I can write some code which loops the original list and add...

Returning data from database in .net: Return a DataTable or LIst<T>?

I'm struggling with bridging the concepts of good database design with good object orientated design. Traditionally if I wanted to display a list of news stories in a repeater, I would use something like: <script runat="server"> void ShowNews() { rptNewsStories.DataSource = News.GetAllNews(); // Returns a DataTable r...

Why does Java's Hashtable's get method take an Object as a parameter?

Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic From the JDK Documentation, the put method: public V put(K key, V value) but, the get method: public V get(Object key) Any ideas? Note: In some code I inherited, there is a bug, where someone used a String as a parameter to the get...

java: Use of Raw type as method parameter errases all parametrized type information in parameter members.

Please explain me why if I use the raw type A in the method test() , the get() method on my typed list returns an Object and not a B.: public class test { public class B{} public class C{} public class A<T extends C> { private List<B> aBList; public List<B> mGetBList() { return aBLis...

Generic method to recive any type of object and convert the object to a List<string>

Hi, EDIT AGAIN: the solution was probably different from my original question. Thanks everyone very much your great ideas. I wish I could vote for more than one answer. EDIT: I am populating a Jquery table plugin from datatables/.net and it requires the data (Json) to be in a certain format like this; "sEcho": 1, "iTotalRecord...

Generic 'TThis' for fluent classes

I'm constructing a fluent interface where I have a base class that contains the bulk of the fluent logic, and a derived class that add some specialized behavior. The problem I'm facing is the return type of the fluent methods in the base class when called from an instance of the derived type. After invoking a method of the base class, on...

C# generics problem - newing up the generic type with parameters in the constructor

I am trying to create a generic class which new's up an instance of the generic type. As follows: public class HomepageCarousel<T> : List<T> where T: IHomepageCarouselItem, new() { private List<T> GetInitialCarouselData() { List<T> carouselItems = new List<T>(); if (jewellerHomepages != null) { ...

Is there a way to find an interface implentation based on a generic type argument?

I have a data access library that has a few classes that all implement the same interface, which has a generic type parameter: public interface IGetByCommonStringRepository<TEntity> { TEntity GetByCommonStringColumn(string commonString); } public class Repository1<Entity1> : IGetByCommonStringRepository<Entity1> { public Entity...

Get the symmetric difference from generic lists

I have 2 separate List and I need to compare the two and get everything but the intersection of the two lists. How can I do this (C#)? ...

generics error: not applicable for the arguments

Can someone explain to me why the following code does not work? public class Test { interface Strategy<T> { void execute(T t); } public static class DefaultStrategy<T> implements Strategy<T> { @Override public void execute(T t) {} } public static class Client { private Strategy<?> a; public void setStrategy(Strat...

invoking delegate with generics arguments in c#

I have a class: public class MyClass<T> { public string TestProperty { get; set; } } and I want to create a delegate to run on instances of this class, such as: Action<MyClass<object>> myDelegate = myclass => myclass.TestProperty = "hello"; However, the above delegate can't be invoked with anything other than a MyClass<object>,...

Dependency Injection and Generic Collections

I'm going round in circles at the moment trying to get the pattern right for using Dependency Injection with a number of IEnumerables. I have three types of object that I want to return from my database: Projects, Batches and Tasks. I want to create a Repository that has the following form: public interface IRepository<T> { IEnume...

Why are TGeneric<Base> and TGeneric<Descendant> incompatible types?

Hi, I have started using of generics in Delphi 2010 but I have a problem when compiling this piece of code: TThreadBase = class( TThread ) ... end; TThreadBaseList<T: TThreadBase> = class( TObjectList<T> ) ... end; TDataProviderThread = class( TThreadBase ) ... end; TDataCore = class( TInterfacedObject, IDataCore ) private FProvid...

Defining an extension method on IEnumerable<T> where T is specific kind of type?

I want a method to only work on types which implement the /, +, -, * operators. Is there any "clean" way to do this? ...

Abstract Generic class

Hi, I have the following class : public abstract class Step { public abstract <S,T> S makeAStep(S currentResult, T element); } and I'm trying to Implement it so it will take two int's and return the sum of them , something like this : public class sumOfInts extends Step { public <Integer,Integer> Integer makeAStep(Integer cu...

using xsd.exe to generate xsd schema for a generic class.

Hello Everyone, I recently discovered the cool abilities of xsd.exe to generate schemas from types in a dll. However I am having trouble generating them for generic types. For example what command line paramters would I use to generate a schema for List? If it matters, I am using these schemas with linq to xsd to create strongly type...