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...
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...
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(), ...
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...
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...
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>
{
...
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...
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...
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...
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 ...
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...
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?
...
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?
...
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...
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...
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...
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...
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...
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...
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...