generics

How does Hibernate find the generic type of a collection in a @OneToMany mapping?

Given a simple entity relationship: @Entity public class Single { @OneToMany public Set<Multiple> multiples; } How does Hibernate find out that the generic type of multiples is Multiple? This information is impossible to find with the standard Reflection API. I'm looking through the source code, but don't really know where to st...

C# generic "where constraint" with "any generic type" definition?

Let me give example: I have some generic class/interface definition: interface IGenericCar< T > {...} I have another class/interface that I want to relate with class above, for example: interface IGarrage< TCar > : where TCar: IGenericCar< (any type here) > {...} Basically, I want my generic IGarrage to be dependent on IGenericCar,...

enums and generic methods in java

Hi all, I still have trouble with some corner cases in the java generics system. I have this method (I'm only interested in the signature) : interface Extractor<RETURN_TYPE> { public <U extends Enum<U>> RETURN_TYPE extractEnum(final Class<U> enumType); } (think about an interface whose implementations sometimes extracts an En...

ambiguous/conflicting constructors in generic class

I've got a generic class: public class BaseFieldValue<T> { public BaseFieldValue() { //... } public BaseFieldValue(string value) { //... } public BaseFieldValue(T value) { //... } } Fine. Except... var myValue = new BaseFieldValue<string>("hello"); Oops. The undesired co...

Why does this function overloading is not working?

Hy, i know it sounds a very stupid question. Here's what i found: public static List<SomeDTO> GetData(Guid userId, int languageId) { // Do something here } public static List<int> GetData(Guid userId ,int iNumberOfItems) { var result = GetData(userID,0); return (from r in result select c.id).Take(iNumberOfItems)...

Generics: constraints on nullable types

The following doesn't compile public static T Retrieve<T>(this NameValueCollection collection, String key) where T : Object { if (collection.AllKeys.Contains(key)) { try { val = (T)Convert.ChangeType((object)collection[key], typeof(T)); } catch { } } return val; }...

Dynamically adding items to a List<T> through reflection

Lets say I have this class class Child { public string FirstName { get; set; } public string LastName { get; set; } } class Container { public List<Child> { get; set; } } I'm working on a deserializer of sorts and I want to be able to create and populate the Child list from the data retrieved. I've gotten this far (I've c...

Java: Problems with TreeSet

I have a class Odp. I want to use TreeSet to keep a sorted collection of Odp objects. However, I've been having problems. public class OdpStorage { private TreeSet<Odp> collection = new TreeSet<Odp>(); public addOdp(Odp o) { return collection.add(o); } public int size() { return collection.size();...

Cast from filtered custom List<T> with LINQ

I have a custom list which inherits from Generic.List<T> like this: public class TransferFileList<T> : List<TransferFile> { .. } When I set (where 'Files' is a TransferFileList<T>): var files = uploadResponse.Files.Where(x => !x.Success).ToList() the 'files' object resolves as System.Collections.Generic.List<TransferFile>, not Tran...

ICollection vs ICollection<T>- Ambiguity between ICollection<T>.Count and ICollection.Count

Note: This is similar, but not quite the same as this other question I've implemented an IBusinessCollection interface. It dervies from both ICollection<T>, and the old-busted non-generic ICollection. I'd prefer to just dump the old busted ICollection, but I'm using WPF databinding with a CollectionView which wants me to implement the o...

Java Class.cast() vs. cast operator.

Having being taught during my C++ days about evils of the C-style cast operator I was pleased at first to find that in Java 5 java.lang.Class had acquired cast method. I thought that finally we have an OO way of dealing with casting. Turns out Class.cast is not the same as static_cast in C++. It is more like reinterpret_cast. It will...

C# - Returning an Enum? from a static extension method

I've added some extension methods for strings to make working with some custom enums easier. public static Enum ToEnum<T>(this string s) { return (Enum)Enum.Parse(typeof(T), s); } public static bool IsEnum<T>(this string s) { return Enum.IsDefined(typeof(T), s); } Note -- because of limitations of generic type constraints, I ...

Modfying a collection (Generics) in a persistent object leads to exceptions or loss of data

What I have? An object that is saved in a static variable and called whenever needed This object interfaces with another application. I have two collections (Generic Lists) in this object Logs And "Data That Is PreFeteched" to be used later Problem is when more than one person is trying to use this object (the object interfaces with...

Using a reference to generic object as a parameter

Hi, I am having some troubles passing a reference to an object which is of generic type. I have found a way around by creating a 'Object' and passing a reference to that rather than the original - but it seems to smell a bit to me. Is there a better way here or do I have to live with it? I understand the first error but the second elu...

C++/CLI: Boxing and Generic Lists

I am trying to create a generic list of references to PointF objects. (No, I am not looking to create a generic list of PointF objects.) However, the following line fails to compile: Generic::List<PointF^> ^pointList; // Generates error C3225 On the other hand, creating an array of PointF references works without a problem as follows:...

Same method that takes a different parameter type?

I know there are very similar questions but im not sure that any of them are exactly what i need. I have 2 methods that do exactly the same thing (so i dont need to override or anything) the only difference is the parameter and return types. public List<List<TestResult>> BatchResultsList(List<TestResult> objectList) { } public List<...

In generic method <T> doSth(List<T> l), check whether T implements Comparable?

The title basically says it all: if I have a java method that is generic in T, can I find out anything about T? In particular, can I check whether T implements a certain interface or extends a certain class? I would like to do something like public <T> List<T> doSth(List<T> l) { if(T extends Comparable) { // do one thing } el...

using generics in c# extension functions

I am using generics to translate Java code to C# and having trouble with containers of the sort: public static class MyExtensions { public static void add(this List<object> list, object obj) { list.Add(obj); } public static void add(this List<string> list, string s) { list.Add(s); } } It seems t...

Retrieve a list of object implementing a given interface

Intro I am building a plugin architecture in my app. The plugins implement a given Interface IBasePlugin, or some other interface which inherited from the base interface: interface IBasePlugin interface IMainFormEvents : IBasePlugin The host is loading the plugin assemblies, and then creates the appropriate object of any class implem...

Recursive Generic Usage

Edited:" I received a very pertinent answer from 'erickson', but there is a side problem (up-casting?) that was not explicitly covered in my original example and is not solved with his answer. I've extended the example to cover this other problem, and I've included it at the end of this post. Thanks for your help. I'm currently facing a...