generics

c# - How to deserialize a generic list<T> when I don't know the type of (T)?

Hi, for auditory reasons I stores the arguments of the business methods serialized into the database using the binaryformatter. The problem is that when an argument is a generic list I don't find the way to cast the deserialized object because I don't know the type, or If I will know the type I don't know how to cast the object at runt...

Is there equivalent of <? extends T> <? super T> in c++?

Is there equivalent of <? extends T> <? super T> in c++? Also, does <? extends T> <? super T> work even if T is an interface in Java? ...

Forcing F# type inference on generics and interfaces to stay loose

We're gettin' hairy here. I've tested a bunch of tree-synchronizing code on concrete representations of data, and now I need to abstract it so that it can run with any source and target that support the right methods. [In practice, this will be sources like Documentum, SQL hierarchies, and filesystems; with destinations like Solr and a c...

Java generics issue with Class

Can't figure out this generics problem. I have these interfaces: public interface LoadableObject { } public interface LoadableObjectFactory<T> { } And now I want to do this: public class ObjectReference<T extends LoadableObject> { Class<? extends LoadableObjectFactory<T>> _cls; public ObjectReference(LoadableObjectFactory<T>...

Java generic parameter itself using generics?

I'm trying to figure out how to structure a program using Java's generics, and wondering if I am doing something fundamentally wrong or just missing a simple bug in my code. Say I have a generic class: public interface Handler<T>{ public void process(T t); } Another generic class takes Handler as a generic parameter (pseudo code):...

C# Generics and polymorphism: an oxymoron?

I just want to confirm what I've understood about Generics in C#. This has come up in a couple code bases I've worked in where a generic base class is used to create type-safe derived instances. A very simple example of what I'm talking about, public class SomeClass<T> { public virtual void SomeMethod(){ } } public class DeriveFr...

C# Generics, Constrain to Specific Structs

Is it possible to constrain a genericised method to accept only specific types of struct? This is OK I believe: string Add<T>(object value, T expiration) where T : struct; but this isn't it appears: string Add<T>(object value, T expiration) where T : Struct1, Struct2; Note: the structs I wish to constrain it to are DateTime or Tim...

generic programming in java

I have some confusion about generic programming in java: If Manager is subclass of Employee, Collection<Manager> managers=new Collection<Manager>; Collection<Employee> employees=managers;//why illegal? Why the last statement is illegal? Since according to illustration in the book CORE JAVA,after the erasion,Collection<Manager> and...

Generic Arrays in Java

Ok, I've been google'ing the web and I just can't seem to find any solution to my problem. I found lots of solutions, just not any that fit. I need to create an array of generics. But the generic type itself extends Comparable. When I try the following : public class Hash<T extends Comparable<String>> { private T[] hashTable; priv...

XML alternative of Text.JSON.Generic for Haskell

Is there any XML-(de)serializer for Haskell using Data/Typeable with functions similar to toXml :: Data d => d -> XmlValue fromXml :: Data d => String -> Result d in the spirit of Text.JSON.Generic? ...

Why do we have generics when we can store things(values and ref) in a ArrayList?

Possible Duplicate: What is cool about generics, why use them? DUPLICATE: http://stackoverflow.com/questions/77632/what-is-cool-about-generics-why-use-them My basic question is same as title. But I want to know something more that can enlightened me with the world of C# generics. Thanks! Aakash ...

Overloading, generic type inference and the 'params' keyword

Hi, I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething<T>(IEnumerable<T> items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(IEnumerable<T> items)"); } Now, I know that this method will often be called with a small numb...

Need help with C# generics

Hi guys, I'm having a bit of trouble writing a class that uses generics because this is the first time that I have had to create a class that uses generics. All I am trying to do is create a method that converts a List to an EntityCollection. I am getting the compiler error: The type 'T' must be a reference type in order to use it as ...

C#: Generic types that have a constructor?

Hello. I have the following C# test code: class MyItem { MyItem( int a ) {} } class MyContainer< T > where T : MyItem, new() { public void CreateItem() { T oItem = new T( 10 ); } } Visual Studio can't compile it, the error is at line where 'new' is used: 'T': cannot provide arguments when creat...

Returning a List<SuperType> on a WCF service

Hey, I am trying to get a WCF service to return a List that contains instances of classes that inherit from A but am getting "The underlying connection unexpectedly closed" when the service returns the list. I have the following [DataContract] [Serializable] public class A { ... } [DataContract] [Serializable] public clas...

Java Generic Type question.

Hi all, i just have a quick question about the Generic Type. i have an interface class public interface myInterface<T> { T add(); } and a sub class public class interfaceImp<T> implements myInterface { private T t1; private T t2; interfaceImp(T t1, T t2){ this.t1 = t1; this.t2 = t2; } public...

Java Collections.sort - help me remove the unchecked warning

List<Question> questions = new ArrayList<Question>(); questions.addAll(getAllQuestions()); //returns a set of Questions Collections.sort(questions, new BeanComparator("questionId")); //org.apache.commons.beanutils.BeanComparator Under Java 1.5, the above works fine except that the 'new BeanComparator("questionId")' generates an uncheck...

Which Java generic should be used in ambiguous cases?

I'm having some problems with a Wicket 1.3 -> Wicket 1.4 migration, but this question could be applied to Java generics overall, too. The migration has caused hundreds of warnings to spring up out of nowhere -- for those unfamiliar with Wicket, many Wicket classes are derived from a common ancestor, which became generified in v1.4 -- an...

ClassName<Type.GetType("Somenamespace.ClassName")>

Basically what I need is to use the Type that i got using Type.GetType in a Generic Type, is it possible, if yes how ? I need something like this: Type t = Type.GetType("mynamespce.a.b.c"); var x = GenericClass<t>(); Duplicate ...

C# -Generic Extension Method

I want to develop a Generic Extension Method which should arrange the string in alphabetical then by lengthwise ascending order. I mean string[] names = { "Jon", "Marc", "Joel", "Thomas", "Copsey","Konrad","Andrew","Brian","Bill"}; var query = names.OrderBy(a => a.Length).ThenBy(a => a); I am new to generics. What ...