generics

Generic lookup function as parameter

I'm trying to clean duplicate code. The only difference are calls like MyType x = Foo.LookupService<MyType>(); vs. MyType x = Bar.FindService<MyType>(); So we have two mehtods of type T xxx<T>(), i.e. a method returning an instance of T given the class parameter T. How could I pass such functions as a parameter to a method that tri...

generics, method signatures, assignments

I thought I understood this but obviously not... I have a method signature like so: void doSomething(List<TypeA> typeAs){...} List<TypeA<TypeB>> getTypeBTypeAs(){...} but if I try and call doSomething(getTypeBTypeAs()); I get a compile error: "the method doSomething(List) in the type ... is not applicable for the arguments (List>)"...

Extension Method for Generic Class.

Possible Duplicates: C# -Generic Extension Method How do you write a C# Extension Method for a Generically Typed Class Is it possible to declare extension methods for generic classes? public class NeedsExtension<T> { public NeedsExtension<T> DoSomething(T obj) { } } ...

C# - Get the implemented type from a Generic Base Class

I may of worded the title completely wrong, but basically, I have the following base class: public class ScheduleableService<T> : ServiceBase where T : IJob { var x = typeof(T); } The implementation of this is something like: public class MyScheduledService: ScheduleableService<MyScheduledJob> { //MyScheduledJob i...

Generic class of generic class in Java

I had a class hierarchy setup like so: public abstract class GameController public abstract class Game I wanted to use a generic GameController class so it takes specific Game subclasses, so I changed it to: public abstract class GameController<GameType extends Game> public abstract class Game But then I also wanted to have a gener...

Using the `is` operator with Generics in C#

I want to do something like this class SomeClass<T> { SomeClass() { bool IsInterface = T is ISomeInterface; } } What is the best way to something like this? Note: I am not looking to constrain T with a where, but I would like my code to be aware of what types of interfaces T implements. I would prefer that I dont hav...

Warnings in Java when casting to a generic type

I have some generic code which I cannot figure out how to legitimately prevent getting warnings from; I am using @SuppressWarnings("unchecked") for the moment, since it seems that casting a generic type can't be done without warnings. How can I get rid of the annotation? What I have is: public MyObject(SharedContext<Object> ctx) { ...

List with different types

Hello =) I'm new at the C# thing.... (.net 3.5) I want a Dictionary to hold two different types of object, one of the type is generic. while iterating through the list, i will call methods like add and clone. I have tried it with a base class and subclasses.... namespace ConsoleApplication1 { class Element{ } class Child1...

Java List generics syntax for primitive types

I want to make a growable array of bytes. I.e a list. In c# would usally do the following syntax List<byte> mylist = new List<byte>(); where as in java this syntax does not work and I have googled around and found the below code List myList = new ArrayList(); but that is not what I want. Any idea's where I am going wrong? ...

How can I iterate a list of lists in scala?

hello, I am trying to implement my own generic flatten for list objects which hold lists in scala. At this point I have def myFlatten[T](list: List[List[t]]): List[T] = { for (xs <- list) for (x <- xs) yield x } I am getting message for xs found Unit required list. ...

How to reflect types that have an interface of generic, and get that type of generic

My 1st objective is to filter the types based on a specific interface with a generic. My 2nd objective is to obtain the type of the generic parameter itself. public UserService : IUserService, IDisposable, IExportableAs<IUserService> { ... } I cannot assume the structure of the class, its interfaces (if any at all) or alike. The...

What is the use of returning List<?> in a method ?

e.g. public interface CacheClient { List<?> getKeysWithExpiryCheck(); } Or should I return List<Object> ...

Generics and reflection in Java

This is probably a very basic question, but I'm really new to generics in Java and I'm having a hard time altering my thought process from the way things are done in C#, so bear with me. I'm trying to build a generic repository in Java. I've created an IRepository interface that looks like this: public interface IRepository<T extends I...

Working Generic Lists of Base Types and Inherited Types

Basically, I'm using an ORM (specifically LLBLGen) that creates entity objects for all my tables. All these entity types inherit from a base class (or really a set of them). I want to create an extension method that accepts a List of the base class and returns some string but I want to pass in inherited types without explicitly casting. ...

strange "template-like" syntax (generics?)

In the following line Graph<Number,Number> ig = Graphs.<Number,Number>synchronizedDirectedGraph( new DirectedSparseMultigraph<Number,Number>()); could you please explain what Graphs.<Number,Number>synchronizedDirectedGraph means ? It looks like a call to a method Graphs.synchronizedDirectedGraph, but the ...

Can someone explain what does <? super T> mean and when should it be used and how this construction should cooperate with <T> and <? extends T>?

I'm using generics rather long time but I've never used construction like List<? super T>. What does it mean? How to use it? How does it look after erasure? I also wonder: is it something standard in generic programming (template programming?) or it's just a java 'invention'? Does c#, for example, allow similar constructions? ...

Java generics + static factory methods = [panic]

Hello, everyone! I thought, I would understand Java generics by now. But now I'm helpless again. I have a generic class where a c-tor constructs correctly-typed instance, while a static factory method produces a type mismatch. Please look at the following code: public class _GenericFactoryMethods { public final static class DemoCl...

Generics + XML Serialization + Custom Objects

I'm trying out Generics and I had this (not so) great idea of creating an XMLSerializer class. The code I pieced together is below: public class Persist<T> { private string _path; public Persist(string path) { this._path = path; } public void save(T objectToSave) { XmlSerializer s = new XmlSerializer(...

Nested generic type parameters - how to handle it?

I created some methods that use nested type parameters generic types in the parameter declaration: public void Foo(IList<Pair<double, IList<double>>> myParameter) { // code goes here } What I wanted to achieve was to force this method to accept 4 types of variables: List<Pair<double, List<double>>> myVar List<Pair<double, double[]>...

Java override Object equals() method

How do I override the equals method in the object class? i.e I have class Person{ //need to override here public boolean equals (Object obj){ } I want to convert the parameter obj to a type Person, but if I do (Person) obj it won't work. ...