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...
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>)"...
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)
{
}
}
...
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...
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...
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...
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) {
...
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...
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?
...
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.
...
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...
e.g.
public interface CacheClient
{
List<?> getKeysWithExpiryCheck();
}
Or should I return
List<Object>
...
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...
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.
...
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 ...
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?
...
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...
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(...
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[]>...
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.
...