generics

Write from Java generic T[] to DataOutput stream?

Given a generic array T[], where T extends java.lang.Number, I would like to write the array to a byte[], using ByteArrayOutputStream. java.io.DataOutput (and an implementation such as java.io.DataOutputStream appears close to what I need, but there is no generic way to write the elements of the T[] array. I want to do something like By...

Java collections covariance problem.

Lets say we have a program which contains such classes: public interface AbstractItem { } public SharpItem implements AbstractItem { } public BluntItem implements AbstractItem { } public interface AbstractToolbox { //well the problem starts here... public List<AbstractItem> getItems(); } public ExpensiveToolbox implements Abstr...

Is this a correct syntax for generic delegates and events?

I'm reading the msdn library topic about genrics . There is an example of declaring event wiht generic delegates, but is it correct? // Code block 8. Generic event handling public delegate void GenericEventHandler<S,A>(S sender,A args); public class MyPublisher { public event GenericEventHandler<MyPublisher,EventArgs> MyEvent; pu...

Is there a way to determine if a generic type is built from a specific generic type definition?

I've got a generic method: Func<IEnumerable<T>, bool> CreateFunction<T>() where T can be any number of different types. This method does a bunch of stuff using reflection and if T is an IDictionary, regardless of the the dictionary's TKey and TValue I need to execute dictionary specific code. So the method could be called: var f = C...

Why would this code complain about "the arity of the generic type definition"?

I've got a generic type: class DictionaryComparer<TKey, TValue> : IEqualityComparer<IDictionary<TKey, TValue>> And a factory method that will (should) create an instance of this class for a given dictionary type. private static IEqualityComparer<T> CreateDictionaryComparer<T>() { Type def = typeof(DictionaryComparer<,...

Inheritance in generic types

Can anyone help me in using Where for generic types? I was trying to create a function which does ST with a number of type double or int, so I said it should be generic function. But when I try to assign a value to variables of that generic type, I can't because it's not a numerical type. Also, I can't use Where to inherit generic type f...

Java Generic Primitive type n-d array

I have to pass a primitive 2d array to a filtering routine.The algorithm for filtering(median filter) is same irrespective of the type of the array.Is there a way to pass any type of array in a generic manner or should I overload the same same function with different array types.In the second case the same code will have to be repeated f...

Generalizing the column name in linq.Where or .OrderBy

I have a "manager" class with a number of sub-classes. I find one particular method being duplicated in all or almost all of the sub-classes, so I want to generalize it. In one case, it looks like this: var Results = from j in Job.All( ) where guids.Contains( j.Job...

Generic enum parameter in java. Is this possible?

Hi, I am trying to write a generic function that will accept any enum, and put the values into a map for use in a drop down. This is what I have so far, (for a specific enum), can my function enumToMap be rewritten generally to accept any enum type? import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import ...

Generic type of a child class doesn't allow parents

Given a structure like this: class Parent { } class Child : Parent { } I have a method that takes a generic type with a constraint that the object is of type Child static void doSomething<T>() where T : Child { if (typeof(T) == typeof(Parent)) { /* ... */ } else if (typeof(T) == typeof(Child)) { /...

Collection of generic types

Hi folks, I have an object (form) which contains a collection (.Fields) which I want to contain instances of a generic class (FormField). The FormField, simply, is defined as such: public class FormField<T> { private Form Form; public T Value { get; set; } public string Name { get; set; } public void Process() { ...

Guice generics - how can I make it less ugly?

I have an interface Producer<T> and a concrete FooProducer that implements Producer<Foo>. Binding this in guice looks ugly as sin: bind(new TypeLiteral<Producer<Foo>>() {}).to(FooProducer.class); I have lots of these such bindings. I have tried the following: static <T> TypeLiteral<Producer<T>> producer() { return new TypeLiteral...

Difference between: <T> ReturnContainer test() and: <T> ReturnContainer<T> test() ?

What is the difference between the signatures of the two methods below? public static <T> ReturnContainer test(Class<T> incomingClass) { List<TestTuple<T>> tupelo = new ArrayList<TestTuple<T>>(); ReturnContainer rc = new ReturnContainer(tupelo, incomingClass); return rc; } What's the difference between requiring a return t...

C# Generics to avoid code repetition?

I am fairly new to C# coming from Java, and I'm wondering if there's a simple way to avoid code repetition involving primitive types like this: private Boolean AtLeastOneBufferItemIsNonZero(int[] Buffer) { Boolean result = false; foreach (int Item in Buffer) { result = !(Item == (int)0); if (result) break; ...

Implementing multiple generic interfaces - type error

I'm trying to do something like this: public interface IRepository<T> { T Get<T>(int id); } public interface IFooBarRepository : IRepository<Foo>, IRepository<Bar> { } IFooBarRepository repo = SomeMethodThatGetsTheActualClass(); Foo foo = repo.Get<Foo>(1); I'm getting a warning: Type parameter 'T' has the same name as the type pa...

Why does this generic constraint compile when it seems to have a circular reference

I have written an extension method in csharp for an MVCContrib Html helper and was surprised at the form of the generic constraint, which on the face of it seems to circularly reference itself through the type parameter. This being said the method compiles and works as desired. I would love to have someone explain why this works and i...

Generic KeyValuePair and Type Inference

Possible Duplicate: Why can't the C# constructor infer type? Why is the following true: var foo = new KeyValuePair(3,4); //doesn't compile! var boo = new KeyValuePair<int,int>(3,4); //works fine! I would think both lines would be legal, since the type can be (should be) inferred from the parameters. Explanation? ...

Java Generic / Type Dispatch Question

Consider the following program: import java.util.List; import java.util.ArrayList; public class TypeTest { public static class TypeTestA extends TypeTest { } public static class TypeTestB extends TypeTest { } public static final class Printer { public void print(TypeTest t) { System.out.prin...

Type conversion with Generic List and Interfaces in C# - Looking for an elegant solution.

Take the following example (created purely to demonstrate the point). What I am trying to do is isolate one part of the system from another and only want to expose a specific subset of the functionality externally from the assembly while internally working against the full objects methods. This code compiles, but I get an invalid cast ...

Generic Class & Type.GetType()

Hi All, Bit of a puzzler, I have a generic class public abstract class MyClass<T> : UserControl { } and I have got a type like this Type type = Type.GetType("Type From DB as String", true, true); and I want to create and instance of MyClass using the type... But this doesn't work. MyClass<type> control = (MyClass<type>)LoadCon...