generics

Why can't e.g. List<ChildClass> be passed to a method that takes a List<ParentClass> as parameter?

Simple example: public class Person { String name; } public class VIP extends Person { String title; } And then doing: public static void main(String[] args) { Person p = new Person(); p.name = "John"; VIP vip = new VIP(); vip.name = "Bob"; vip.title = "CEO"; List<Person> personList = new ArrayList<...

Lambda on List<T> and using Reflection to get the property names

Let's say you have a Generic Class which have a List<T> Items; Now think of this basic lambda expression: var result = Items.FindAll(x => x.Name = "Filip"); This will only work as long as we Know the Properties of T, which you don't when it's a generic type. Therefore I would like to fetch the properties using Reflection like this:...

Implementation of IEnumerable<T> works for foreach but not LINQ

I currently use the following base collection. abstract class BaseCollection : Collection<BaseRecord> { } I would like to replace it with the generic collection below. This however is not a trivial task due to the amount of derived classes that currently implement BaseCollection. abstract class BaseCollection<TRecord...

Delphi 2010 Generics of Generics

Hi, How can I store generics in a generics TList holded by a non generic object ? type TXmlBuilder = class type TXmlAttribute<T>= class Name: String; Value: T; end; TXmlNode = class Name: String; Attributes: TList<TXmlAttribute<T>>; Nodes: TList<TXmlNode>; end; ... end; The compi...

C# Single Generic Collection Method for Multiple Derived Classes.

Scenario I have a single base class and 2 (possibly 3) other classes that derive from that base class. //Base Class class RateInstrument { public string Ric { get; set; } public string Tenor { get; set; } public double Rate { get; set; } public DateTime Date { get; set; } public double Price { get; set; } ...

how to return an IList with members of an arbitrary type

I'm not sure this is even possible, but here goes: I have a class Zoo which holds a dictionary of Animal Type -> List of Animals. e.g. Cat Type -> {Cat1, Cat2} Zebra Type -> {Zebra1, Zebra2} Cat and Zebra are subclasses of Animal. Now Zoo has a method IList<Animal> GetAnimalsOfType(Type type). I'd like the return value to be of the t...

Narrowing return types on inheritance (generics involved)

I'm wrestling with a bit of weird generics behavior regarding being able to "narrow" return types when subclassing. I managed to reduce the problem to the following set of classes: public class AbstractIndex { } public class TreeIndex extends AbstractIndex { } public interface IService<T extends AbstractIndex> { } public interface IT...

What mistake am I making when creating a constraint in generics?

In asp.net 2.0 I have several "dropdowns" defined using generics (examples eye color, hair color, etc). The fields are all typical; id, text, etc. All are defined as their own classes which must implement an interface I created called ILookup. However, when I try to return a List<> of this class using: ddlEyeColor.DataSource = luMgt.Get...

Why is compiler not seeing my generic constraint?

ASP.NET 2.0; I am attempting to pass a custom type to a generic list to get a list of whatever type I send in. However I keep getting this error: Type argument '' does not inherit from or implement the constraint type '' What's confusing about this is I AM implementing the constraint. In fact, here is the Interface: Public Interface I...

Creating a generic method in C#

I am trying to combine a bunch of similar methods into a generic method. I have several methods that return the value of a querystring, or null if that querystring does not exist or is not in the correct format. This would be easy enough if all the types were natively nullable, but I have to use the nullable generic type for integers a...

NHibernate and XML Serialization with IList<T>

I've recently started using NHibernate, and on the whole like it a lot. Until I ran into a problem with needing to serialize to XML and back. I have a class that has a many to many relationship, so have an IList in the parent class to hold the list of child objects. Class parentClass{ IList<childClass> childList; string varA; s...

Generic Sorting on List<T>

Hello, I have the following code: public class OMyObject { public int Id { get; set; } public string Value { get; set; } public DateTime? MyDate { get; set; } } I also have this code: public static class ObjectExtension { public static List<OMyObject> Sort<T>(this List<OMyObject> o, Func<OMyObject,...

[Java] Generics question.

Suppose I have: public interface Action<S extends Shape> { public void start( S shape ); } Why do I get the following? public <S extends Shape> void performAction( Action<S> action, Shape shape ) { action.start(shape); // error: cannot supply Shape } In other words, in the future, I might have subclasses of Shape and Actions t...

Why can't I use wildcard with methods receiving a parameterized argument?

For example, I use a method Measure.doubleValue(Unit<?> unit) which returns the double value of a measurement, expressed in the specified Unit. If I pass a Unit<?> variable to it, I get the still very cryptic (to me) error message: The method doubleValue(Unit<capture#27-of ?>) in the type Measurable<capture#27-of ?> is not app...

Code Contracts: Is it possible to supply a contract class for a generic interface?

I have just got started using Microsoft's Code Contracts and already ran into a problem that I can't solve. Let's take this interface for which I'd like to specify a contract: public interface IRandomWriteAccessible<T> { T this[uint index] { set; } uint Length { get; } } The documentation says to use the ContractClass attribu...

Using Moq .GetMock to register ~1IRepository with Linq Expression?

Is it possible to Mock a Linq Expression via Moq using a Generic class such as ~1Repository. ~IRepository being something that is injected via an IoC such as StructureMap or Windsor? CODE TO TEST: var person = _repository.Find<Person>() .Where(p => p.Id == person.Id).SingleOrDefault(); TEST: var rep...

Can we make a enum as a generic data type?

Can we make a enum as a generic data type? If so please provide an example. Thanks in advance. ...

Passing an Interface collection

Suppose you have the following class: class Car : IPainting { ... } Then a function like this: void AddCars(IEnumerable<Car> collection) Then a code snippet like this: Car bmw = new Car(); Car mercedes = new Car(); IPainting a = (IPainting) bmw; IPainting b = (IPainting) mercedes; IPainting[] paintings = new IPainting[] {a, b};...

Generic LINQ TO SQL Query

Lets say I have two L2S classes generated by the designer with one property as follows: public class A { public bool IsActive {get;set;} } public class B { public bool IsActive {get;set;} } I have a generic DataAccess class as follows: public class DataAccessBase<T> where T : class { NWDataContext dataContext = new ...

How to use a generic result as a generic parameter when the type is a wildcard?

[UPDATE] The real situation was a bit more complicated than my initial question made it seem. I've changed the code a bit to reflect that.[/UPDATE] I'm a bit stumped by the following behavior. Given code like: interface Inter<T> { T makeT(); void useT(T t); } public class Foo { public void bar(Qux q) { Inter<?> x = getInterF...