generics

How can I coax an object to a IList<T>?

I am using reflection along with linq.Dynamic, and I am having a small problem with creating a query that needs to get IQueryable<T> from an IList<T> or ISet<T> when I currently have an object. At first I thought I could write a little helper method: object Helper<T>( IList<T> list, string query, param object[] values ) { ... do quer...

Is it possible to constrain a generic parameter to be a subtype of the current object?

Here's an interesting problem that I have just come across. It is possible to do what I want using extension methods, but does not seem possible to do with members of the class itself. With extension Methods it is possible to write a method that has a signature that looks like this: public static void DoStuff<T>(this T arg1, T arg2) ...

Question on Java Generics Bounded Wildcard

Is there a difference between 1) <N extends Number> Collection<N> getThatCollection(Class<N> type) and 2) Collection<? extends Number> getThatCollection(Class<? extends Number>) ...

Create an instance of a Type, provided as a parameter to a method

The class to instantiate: public class InstantiateMe { public String foo { get; set; } } Some pseudo-code: public void CreateInstanceOf(Type t) { var instance = new t(); instance.foo = "bar"; } So far I'm figuring that I need to use reflection to get this done, given the dynamic nature of what I...

Transforming a generic list of one type to another type, where the types are only known at runtime

Essentially, I'm looking for a way to transform GenericList<'TInput> to GenericList<'TOutput>, where GenericList is a generic list of any type that implements a specific interface, and the types TInput and TOutput are only known at runtime. Below is a snippet of a class and method that can perform this operation, where TInput and TOutpu...

How do you get the name of a generic class using reflection?

How do you get the name of a generic class using reflection eg public class SomeGenericClass<T> { } SomeGenericClass<int> test = new SomeGenericClass<int>(); test.GetType().Name returns "SomeGenericClass'1" How do I get it to return "SomeGenericClass" without the '1? ...

Java Reflective Method Call with Generics

Given this method: public final void foo (List<MyClass> bar){ .. } I want to be able to call this method reflectively. In order for getMethod to work, I have to change it to: public final void foo (List bar){ .. } This does not seem right for obvious reasons, but no combination of inputs to getMethod seem to work otherwise. I have ...

How can I serialize (and later deserialize) a generic type in Scala?

Say I'd like to implement something like this: def serialize( list: List[_] ) : Node = { <list> { for ( item <- list ) yield serializeItem(item) } </list> } def deserialize( node : Node ) : List[_] = { // ? } How do I get the type of the List, e.g. T in List[T] so I can write that out? Or do I need it? How can I instantiate ...

How can I get XStream to output Scala lists nicely? Can I write a custom converter?

This code: println(new XStream.toXML(List(1,2,3))) produces this XML: <scala.coloncolon serialization="custom"> <unserializable-parents/> <scala.coloncolon> <int>1</int> <int>2</int> <int>3</int> <scala.ListSerializeEnd/> </scala.coloncolon> </scala.coloncolon> Instead I'd like this: <list> <int>1</int> <...

How do I write a generic method that takes different types as parameters?

I have the following extension method to add the elements in one collection to another collection: public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list) { foreach (var item in list) { collection.Add(item); } } This works fine if the IEnumerable list is the same type as the...

Generic type in, different generic type out using interfaces?

I'm fairly new to generics and I'm having some trouble understanding parts of how they work and also failing to get it to work the way I want. So far, I have this; public interface IObjectMapper { T MapObject<T>() where T : new(); } public class CustomObjectMapper : IObjectMapper { T IObjectMapper.MapObject<T>() { ...

Automapper, generics, dto funtimes

Here's the deal: I have a report designer where users can create reports based on some predefined datasets. They can select a set of columns to include in the report and then, when the report is ran, an IList is created by mapping the NHibernate collection over to the dto class collection using automapper. The problem with this is tha...

Delphi: determine actual type of a generic?

Is there any way to determine the type of a variable passed as an argument to a method? Consider the class: TSomeClass = class procedure AddToList<T: TDataType; U: TListClass<T>>(Element: T; List: U); end; with the method implementation procedure TSomeClass.AddToList<T, U>(Element: T; List: U); begin if Element is TInt then L...

Catching exception types given as type parameters in C# 2.0

Hi, In the following method, the first catch block is never run, even when an exception of type ExceptionType is thrown: /// <summary> /// asserts that running the command given throws an exception. /// </summary> public static void Throws<ExceptionType>(ICommand cmd) where ExceptionType : Exception { ...

Can I do this Generic thing?

Hi there: It seems I'm missing something with Java Generics because something I think is simple, it appears to me that can´t be done. Maybe you can help... This is the scenario: I'm coding a generic abstract DAO with simple CRUD operation so every specific DAO of my application can have it for free: public abstract DefaultDAO<T,V> { ...

Determining Derived Class Type At Runtime

I have a generic method with a new() constraint. I have a call to this method with an abstract type which, of course, won't compile. I wish to keep the call generic, but was hoping I could determine at runtime what the derived class is so as to satisfy the compiler. The call would look something like this: var result = MyGenericMetho...

C# Improved algorithm

I have been asked at interview (C# 3.0) to provide a logic to remove a list of items from a list. I responded int[] items={1,2,3,4}; List<int> newList = new List<int>() { 1, 2, 3, 4, 5, 56, 788, 9 }; newList.RemoveAll((int i) => { return items.Contains(i); }); 1) The interviewer replied that the algorithm i had employed will gradual...

Good introduction to generics

Being compelled by the advantages I'm looking for a way to integrate generic programming into my current programming style. I would like to use generics in C# but can't find any good introductory material with some everyday examples of use. If you have experience with generics: what resources did you find most useful learning them? (book...

Java generics: multiple generic parameters?

Hello, I was wondering if it's possible to write a function that accepts multiple generic types as follows: public int void myfunction(Set<T> a, Set<T> b) { return 5; } Set<Integer> setA = new HashSet<Integer>(); Set<String> setB = new HashSet<String>(); int result = myfunction(setA, setB); Will that work? Does the generic in ea...

Delphi: generics and 'is'-operator problem

Based on an earlier post, I've written the following code. Please excuse the verbosity of this post. I believe it's better for all parties to have the full code available to test and comment on. program sandbox; {$APPTYPE CONSOLE} uses SysUtils, Generics.Collections; type TDataType = class // Stuff common to TInt and TStr ...