generics

How can I use interface as a C# generic type constraint?

Is there a way to get the following function declaration? public bool Foo<T>() where T : interface; ie. where T is an interface type (similar to where T : class, and struct). Currently I've settled for: public bool Foo<T>() where T : IBase; Where IBase is defined as an empty interface that is inherited by all my custom interfaces....

Generic Types vs Abstract class/Interfaces

Suppose we are creating a generic control in .NET. E.g. a tree. I don't understand why people use this generic type definition Control<T> when in Object Oriented Programming I can use an abstract class or an interface: Control<IItem> or Control<BaseClass> So the only thing to do is that, their types must derive from that base clas...

Using LINQ with classes implementing non-generic ICollection

I wanted to run a LINQ query against a MatchCollection object but found this wasn't possible as it doesn't implement ICollection<T>, just ICollection. What is the best option for using LINQ with non-generic collections, both in terms of code conciseness but also performance and memory usage? (If interested, here is the non-LINQuified c...

Method overloads which differ only by generic constraint

I've run into a bit of a problem, which I simply cannot find a good work-around to. I want to have these 3 overloads: public IList<T> GetList<T>(string query) where T: string public IList<T> GetList<T>(string query) where T: SomeClass public IList<T> GetList<T>(string query) where T: struct Obviously the first constraint won't even c...

What is TKey and TValue in a generic dictionary?

Hi, The names TKey and TValue in a dictionary are confusing me. Are they named with that convention for a reason or could they have named it anything? i.e. if I create a generic, do I have to use some sort of naming convention also? ...

Is it possible to call a method on the type you pass into your generic method?

Is it possible to call a method on the type you pass into your generic method? Something like: public class Blah<T> { public int SomeMethod(T t) { int blah = t.Age; return blah; } } ...

How do you provide a default type for generics?

I have a class that currently has several methods that take integer parameters. These integers map to operations that the application can perform. I'd like to make the class generic so that the consumers of the class can provide an enum type that they have with all the operations in it, then the methods will take parameters of that enum ...

Guice SPI: find bindings by wildcard types

Guice provides a means to find all bindings for a given type (Injector#findBindingsByType) and it also provides a TypeLiteral class from which it seems possible to construct a wildcard type. What I would like to do is find all bindings for some type that is parameterised by a wildcard type but I can't figure out how to do it. A look at t...

Struggling with generics and classes

I'm always struggling with generics. I don't know why this makes my go crazy. I have an old problem I've got several times. class Father { ... } class Child1 extends Father { ... } class Child2 extends Father { ... } public class TestClass { private static Class<? extends Father>[] myNiceClasses = new Class<? extends ...

Generic factory

Hi all, suppose I have a TModel: TModelClass = class of TModel; TModel = class procedure DoSomeStuff; end; and 2 descendants: TModel_A = class(TModel); TModel_B = class(TModel); and a factory : TModelFactory = class class function CreateModel_A: TModel_A; class function CreateModel_B: TModel_B; end; Now I want to refactor...

Problem with Observer Pattern and generics in Java

I have made a generic Observer interface and an Observable class, but can't compile my class due to some generics problem. I don't know precisely why what I'm trying to do is forbidden. The code follows: public class Observable<U> { private List<Observer<Observable<U>, U>> _observers = new ArrayList<Observer<Observable...

How to put an interface constraint on a generic method in c# 3.5?

I want to achieve something like this in C# 3.5: public void Register<T>() : where T : interface {} I can do it with class or struct, but how to do it with an interface? Thanks! André ...

Is there a way to dynamically create a type's property names while maintaining design-time intellisense?

The simplest example would be the built in class keyValuePair(of T,U). I would like to be able to name the key property and value property differently depending on the usage and the designtime intellisense show me those names instead of key and value. Could this be done with reflection.Emit? Can it be done another way? This seems like a...

How can I convert to a specific type in a generic version of TryParse()?

I have the following scenario where I want to pass in string and a generic type: public class Worker { public void DoSomeWork<T>(string value) where T : struct, IComparable<T>, IEquatable<T> { ... } } At some point along the way I need to convert the string value to its T value. But I don't want to do a straight convert a...

Java Type Erasure Problem

Hello. I've made an example to demonstrate my problem: Metrical.java public interface Metrical<T> { double distance(T other); } Widget.java public class Widget implements Metrical<Widget> { private final double value; public Widget(double value) { this.value = value; } public double getValue() { return value; } ...

How to tell C# to look in an object's base class for a property?

I'm getting the error "T does not contain a definition for Id" below in the specified line, even though when I debug, I see that "item" does indeed have a property "Id" in its base class. How do I specify here that I want C# to look in the item's base class for Id (and why doesn't it do this automatically?)? //public abstract class Ite...

Java Generics problem with wildcard

Hi, There is any way to fix this situation (I have try to simplyfy the scenario as much as i could): public class Test { public static void main(String [] args) { /* HERE I would like to indicate that the CollectionGeneric can be of something that extends Animal (but the constructor doesn't allow wildc...

How do I use C# Generics for this implementation when the Generic Decision is based on a DB Value

We have a content delivery system that delivers many different content types to devices. All the content is stored in a database with a contentID & mediaTypeID. For this example lets assume the MediaType can be one of these 2 but in reality theres many more of them. Gif MP3 Because the content is stored in different places based on m...

Can I use generics to populate List(of t) with custom classes?

I have several different lists I want to call. They all have the same format for the class: id, value, description, order. Instead of creating a bunch a classes to return the all of the many lists, I wanted to use generics and just TELL it what kind of list to return. However, I can not figure out how to populate the classes. Here are 2...

How to determine the type of the returned value within the generic method

Hello to all, We've created a generic method like so: public TReturnType GetValue(string key) { var configurationParameter = (from cp in _repository.ConfigurationParameters where cp.ConfigurationParameterKey == key select cp).FirstOrDefault(); var returnValue (TReturnType)Convert.ChangeType(configurationParamete...