generics

Generic function to handle disposing IDisposable objects.

I am working on a class that deals with a lot of Sql objects - Connection, Command, DataAdapter, CommandBuilder, etc. There are multiple instances where we have code like this: if( command != null ) { command.Dispose(); } if( dataAdapter != null ) { dataAdapter.Dispose(); } etc I know this is fairly insufficient in terms of ...

Generic instance variable in non-generic class

I'm trying to write a class that has a generic member variable but is not, itself, generic. Specifically, I want to say that I have an List of values of "some type that implements comparable to itself", so that I can call sort on that list... I hope that makes sense. The end result of what I'm trying to do is to create a class such that...

Collection<String>

Whats the main difference between a Collection of String (Collection)and a simple, plain collection? ...

The use of class type parameters in generic method

I have a generic class intended to keep values for different kinds of properties. I want to provide a type-safe way to set property values, so that it is impossible to assign property a value of wrong type. I defined an interface to be implemented for all property kinds: public interface Property<T> { } where type parameter T is us...

upcast from List<subclass> to List<superclass> via List<?>

I have a class A and a class B extends A In another class C I have a field private List<B> listB; Now, for some unusual reason, I have to implement this method in C public List<A> getList(); I tried to do so by forcing an upcast of listB field to List<A> via a List<?> cast: public List<A> getList(){ return (List<A>)(List<?>)l...

Structuremap Generics with two type parameters

Hi, I know I can, in Structuremap, do this: var container = new Container(cfg => { cfg.For(typeof (IDomainDataRepository<>)).Use(typeof (DomainDataRepository<>)); }); but what if my Interface has 2 type parameters: IDomainDataRepository<T,TKey> instead ofIDomainDataRepository<T> How to tell Structuremap to instantiate this type ...

Difference between Java Generic Parameters

What is the difference between passing in generic parameter some generic class with and without his generic parameter? Example: Simple Generic class: public class Foo<T> { /*...*/ } Simple class that extend simple generic class setting the generic parameter to some irrelevant type: public class FooFoo extends Foo<Type1> { /*...*/ }...

trouble in usage of the generic methods

I have a method declared as below <T> T getAdapter(Adaptable adaptable, Class<T> extensionInterface); and I'm calling it with below argumants adapterManager.getAdapter(new AWScoreAdapterImpl(null), AWScoreAdapter.class); Can someone help me understand why the above line is causing the below compile time error The method getAdap...

How do I specify multiple generic type constraints on a single method?

I can restrict generics to a specify type using the "Where" clause such as: public void foo<TTypeA>() where TTypeA : class, A How do I do this if my function has two generic types? public void foo<TTypeA, TTypeB>() where TTypeA : class, A && TTypeB : class, B The above doesn't work. What's the correct syntax to add the rule "TType...

Generics - call a method on every object in a List<T>

Is there a way to call a method on every object in a List - e.g. Instead of List<MyClass> items = setup(); foreach (MyClass item in items) item.SomeMethod(); You could do something like foreach(items).SomeMethod(); Anything built in, extension methods, or am I just being too damn lazy? ...

how can i evaluate generic type in runtime in c#

i need to evaluate generic type at runtime using Type.GetType() Type t = Type.GetType("className"); Table<t> table = dataContext.GetTable<t>(); any ideas please ?? ...

Java: how to convert a List<?> to a Map<String,?>

I would like to find a way to take the object specific routine below and abstract it into a method that you can pass a class, list, and fieldname to get back a Map. If I could get a general pointer on the pattern used or , etc that could get me started in the right direction. Map<String,Role> mapped_roles = new HashMap<String,Role>()...

uniquess of methods and constraints

Hi, I ran in to the following: public void AddConfig<T>(Config c) where T : BaseTypeA { // do stuff } public void AddConfig<T>(Config c) where T : BaseTypeB { // do stuff } I would love to be able to do this. But i think it's impossible. The compiler ignores the constraints. Why? (I know it's by design). I think my 2 options are...

Get Package Name of Generic Type Argument

Hi all How can I get the package name (in general, the class) of a generic type argument? I have a class similar to this: public class MyClass<T> {} What I'd like to do is something like this (non-working pseudo-Java): return T.class.getPackage().getName(); I need this, since JAXB needs this as the context path (JAXBContext.newIns...

Is there any way to constrain a type parameter to EITHER a reference type OR a nullable value type?

It'd be nice if I could define a method like this: public T GetItem<T>() where T : null { if (someCondition<T>()) return someCalculation<T>(); else return null; } Then I could use this on reference types (e.g., object, string) as well as nullable value types (e.g., int?, double?): anything that can be assigned ...

Java generics + Builder pattern

How do I call start() below? package com.example.test; class Bar {} public class Foo<K> { final private int count; final private K key; Foo(Builder<K> b) { this.count = b.count; this.key = b.key; } public static class Builder<K2> { int count; K2 key; private Builde...

binding generic c++ libraries to python with boost.python

I would like to know what's the process, when binding C++ libraries that are written with in a generic way. Is there a posibility of binding a template class, or you can only bind only a template generated class ? ...

Implementation of Lazy<T> for .NET 3.5

.NET 4.0 has a nice utility class called System.Lazy that does lazy object initialization. I would like to use this class for a 3.5 project. One time I saw an implementation somewhere in a stackoverflow answer but I can't find it anymore. Does someone have an alternative implementation of Lazy? It doesn't need all the thread safety featu...

Will the JVM be extended to handle generics?

What do you think? Will the JVM ever get support for generics? Quite likely that would not only require substantial changes to the JVM, but also to the class file format, but languages running on the VM would greatly benefit from it. Edit: The Java language actually supports some sort of generics as a compile time feature, which adds s...

Conversion of IEnumerable<T> for Extension Method Issue

I have the following class and extension class (for this example): public class Person<T> { public T Value { get; set; } } public static class PersonExt { public static void Process<TResult>(this Person<IEnumerable<TResult>> p) { // Do something with .Any(). Console.WriteLine(p.Value.Any()); } } I was ...