generics

Are Generics in D2009 usable in large projects?

Hi, I'm pretty frustrated. I'm using Delphi 2009 and was very happy about the inclusion of generics in this version of Delphi. Everything worked great at the beginning, but now that I use generics all over the place I run into problem after problem - most of the time some internal errors, where I don't even see where exactly they are ca...

Implementing timeout for calling generic function with parameters.

I am trying to get my head around the use of the Action delegate type for use in forcing a timeout when methods called in a 3rd party COM dll hang up. After much searching I find that I can use Action<> or Func<> and pass up to 4 generic parameters depending on whether the method called returns a parameter or not. For this instance I wi...

Compilation Error with Generic Type T when passed to a function twice

I'm probably missing something very basic but I cannot figure out why I get a compilation error with a certain code and I don't get it in an almost identical code. So I do get an error here: //parent.GetChildren() returns a IEnumerable<IBase> F1<T>(T parent, Func<string, T, string> func) where T: IBase { F1(parent.GetChildren(), ...

Is it possible to detect class context in an inherited static method?

OK, that title is a little unclear, but I can't think of a better way of putting it, other than explaining it... Say I have a class Animal, with a static, generic method: public static T Create<T>() where T : Animal { // stuff to create, initialize and return an animal of type T } And I have subclasses Dog, Cat, Hamster etc. In or...

Why does generic method with constaint of T: class result in boxing?

Anyone any idea why a generic method which constrains T to class would have boxing instructions in the generates MSIL code? I was quite surprised by this since surely since T is being constrained to a reference type the generated code should not need to perform any boxing. Here is the c# code: protected void SetRefProperty<T>(ref T pr...

Is it possible to make "this type" for generics in C#?

Kind of theoretical question. Quite long so feel free to skip if you are not in the mood for theory. Imagine that you have two classes, one inherited from another. The base class is generic and has a method that in the closed type must return some instance of this closed type. Like this (note ??? in text): public class Adapter<T> { ...

Reflection.Emit Generic Base Class Generic Method Call

I'm dynamically sub classing a generic type (filling it's contract) that has a generic method. I attempt to call this generic method but the assembly I produce has errors. Reflector crashes when trying to open the assembly and this code snippet does not run. The exception I get is: An attempt was made to load a program with an incorrec...

Is there a workaround for generic type constraint of "special class" Enum in C# 3.0?

Update: See the bottom of this question for a C# workaround. Hi there, Consider the following extension method: public static bool HasFlags<T>(this T value, T flags) where T : System.Enum { // ... } This will, as you may know, throw an error at compile-time, since a class is not normally allowed to inherit from System.En...

Generic Identity Map in C#. Don't want public constructor.

I'm trying to implement an identity map using generics. I have an abstract class, Entity, and a derivation constraint on my map for Entity. Since my map needs to be able to instantiate entities, my map also has a constructor constraint. However, for the map to be useful, Entity subclasses should not be able to be instantiated from clien...

How can I perform a List<object>.Cast<T> using reflection when T is unknown

I've been trying to do this for a good few hours now and this is as far as I have got var castItems = typeof(Enumerable).GetMethod("Cast") .MakeGenericMethod(new Type[] { targetType }) .Invoke(null, new object[] { items }); This returns me System.Linq.Enumerable+d__aa`1[MyObjectType] whereas ...

How do I cast this generic interface?

I have the following classes defined to do validation: public class DefValidator : IValidate<IDef> { } public interface IDef : IAttribute { } Then, I have a list of validators defined as so: IList<IValidate<IAttribute>> ValidationObjects; When I try the following, it doesn't compile saying it can't convert types. DefValidator def...

Interlocked.Exchange can't be used with generics?

I'm writing a generic class where I need to use Interlocked. T test1, test2; Interlocked.Exchange<T>(ref test1, test2); This won't compile. So am I forced to use Exchange(Object, Object) instead even tho MSDN advices not to use it that way? ...

How to call generic method with a given Type object?

I want to call my generic method with a given type object. void Foo(Type t) { MyGenericMethod<t>(); } obviously doesn't work. How can I make it work? ...

refactoring Java arrays and primitives (double[][]) to Collections and Generics (List<List<Double>>)

I have been refactoring throwaway code which I wrote some years ago in a FORTRAN-like style. Most of the code is now much more organized and readable. However the heart of the algorithm (which is performance-critical) uses 1- and 2-dimensional Java arrays and is typified by: for (int j = 1; j < len[1]+1; j++) { int jj = (con...

Using base objects as parameters in a generic function

Hi I'm trying to implement a helper method using generics (C# / 3.5) I've a nice structure of classes, with base classes like so: public class SomeNiceObject : ObjectBase { public string Field1{ get; set; } } public class CollectionBase<ObjectBase>() { public bool ReadAllFromDatabase(); } public class SomeNiceObjectCollection : C...

Adding constraints to an interface property

I'm writing an interface and I want to declare a property that returns a generic collection. The elements of the collection should implement an interface. Is this possible and, if so, what is the syntax. This doesn't compile, what's the right way to do this? interface IHouse { IEnumerable<T> Bedrooms { get; } where T : IRoom } Th...

Trying to understand the point of constraints - am I on the right track?

I think I finally get the point of constraints, but still a bit confused. Can someone tell me if the following is right? Basically, if you inherit a class you may want to make sure that the class you inherit also inherits from some other class or some other interface. It's confusing because presumably you would know that you would o...

Best exception for an invalid generic type argument

I'm currently writing some code for UnconstrainedMelody which has generic methods to do with enums. Now, I have a static class with a bunch of methods which are only meant to be used with "flags" enums. I can't add this as a constraint... so it's possible that they'll be called with other enum types too. In that case I'd like to throw a...

C# Generics Question

I have a couple of areas in an application I am building where it looks like I may have to violate the living daylights out of the DRY (Don't Repeat Yourself) principle. I'd really like to stay dry and not get hosed and wondered if someone might be able to offer me a poncho. For background, I am using C#/.NET 3.51 SP1, Sql Server 2008, a...

Extending Generic Abstract Class & Correct Use of Super

public abstract class AbstractTool<AT extends AbstractThing> { protected ArrayList<AT> ledger; public AbstractTool() { ledger = new ArrayList<AT>(); } public AT getToolAt(int i) { return ledger.get(i); } // More code Which operates on Ledger ... } public class Tool<AT extends AbstractThing> extends A...