generics

Why does this generic cast fail?

I have the following inheritance: internal abstract class TeraRow{} internal class xRow : TeraRow {} // xRow is a child of TeraRow public IEnumerable<TeraRow> Compare(MappedTables which, DateTime selectionStart , DateTime selectionEnd, string pwd) { IEnumerable<xRow> result=CompareX(); return (IEnumerable<TeraRow>)result; /...

Unit tests that inherit from a type that uses generics

I have an interface with generics that is implemented by some classes. For each of these classes there is proxy class that implement the interface as well. Giving roughly this code: public interface ISomeInterface<T> { T SomeProperty { get; } T SomeAction(); } public interface IClassA : ISomeInterface<string> {...

Linq Newbie. Can I write this Linq query more concise?

Dim entName = "Some Auto Dealer" Dim whereEntity As Expression(Of Func(Of Entity, Boolean)) = Function(en) en.ENTY_Name = entName Dim login = Repository(Of Entity).Create().FindSingle(whereEntity) Dim whereDealer As Expression(Of Func(Of Dealer, Boolean)) = Function(dlr) dlr.Entity.Equals(login) Dim dealer = Repository(...

Using generics when type is not known at compile time

Platform: C# 2.0 WinForms I have a factory class that provides an instantiation of a particular data mapper depending on the type that I send it, the code is as such: public static IDataMapper<T> GetMapper<T>() where T: IDto { Type mapperType = MapperLocator.GetMapper(typeof(T)); return (IDataMapper<T>)mapperType.Assembly.Crea...

Is there a way with Java Generics to take Generic parameter that requires implementation of 2 interfaces?

Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1> getChildren(); public void setChildren(List<? extends ChildInterface1> children); } public interface ParentInterface2 { public List<? extends ChildInterface2> getChildren(); public void setChildren(List<? extends ChildInter...

Getting type arguments of generic interfaces that a class implements

I have a generic interface, say IGeneric. For a given type, I want to find the generic arguments which a class imlements via IGeneric. It is more clear in this example: Class MyClass : IGeneric<Employee>, IGeneric<Company>, IDontWantThis<EvilType> { ... } Type t = typeof(MyClass); Type[] typeArgs = GetTypeArgsOfInterfacesOf(t); // At...

Generic class with restricted Type parameter

I want to create a generic class that takes a type parameter and restrict that parameter to numeric types or more generally to any type upon which the increment operator ++ can be applied. I know I can do the following to restrict to structs but obviously there are structs that aren't numeric types and for which the ++ operator is not s...

Why Create a Class Derived from a Generic List (.NET)?

Whats is the difference between: List<MyType> myList; and myList : List<MyType> Its obvious that the first one is list and second one is a class. But my question is what's the advantage of second over first one. ...

Factory Creating Objects According to a Generic Type C#

What would be the most efficient way to instanciate an object according to a generic type passed to a Factory class, for instance: public class LoggerFactory { public static ILogger<T> Create<T>() { // Switch Statement? // Generic Dictionary? // EX.: if "T" is of type "string": return (ILogger<T>)new Stri...

Why does this code with generics compile?

This seems like a stupid question, but I'm tripping over it at the moment. Why does this compile? import java.util.*; public class Test { public static void main (String[] argv) throws Exception { Map<String,String> map = new HashMap<String,String>(); map.get(new ArrayList<String>()); } ...

Design problem causing inability to get returned proper dataType

I have an object that has properties of another object and one called DataValue, but the type that I want DataValue to return depends on information contained in the object in the other property. I'm not convinced my way is the best way to do this. I have this business object called an AssetStructure. An AssetStructure object contains...

Get sum of elements of an Array in C# using generics

I want to write a method which can take an arbitrary array of a numeric type and return the sum of all the elements between startIndex and endIndex. This is what I have: private static T SumArrayRange<T>(T[] scores, int startIndex, int endIndex) { T score = 0; for (int i = startIndex; i <= endIndex; i++) { score += s...

How to use TDictionary?

Hello, By using Google I have found some nice snippets of example code for using TDictionary in Delphi, but have not been able to compile any of them. I use Delphi 2009 Update 1,2,3,4. When I write this: var Dic: TDictionary<Integer,string>; I get "Unknown idenitifier TDictionary<,>" How to use them? ...

C# 2.0 generics: How to create an Action object with zero parameters

First of all, I'm using VS2005 and C# 2.0. I'm trying to set a combobox' Text property from inside the SelectedIndexChanged event. From another thread here on StackOverflow this was proposed done the following way: BeginInvoke(new Action(() => someCombobox.Text = "x" )); Now, first of all this returns a compiler error for me. I belie...

Java Generic Method Question

Consider this code: public <T> List<T> meth(List<?> type) { System.out.println(type); // 1 return new ArrayList<String>(); // 2 } It does not compile at line 2, saying that List is required. Now, if it's changed to: public <T> List<?> meth(List<T> type) { System.out.println(type); // 1 return new ArrayList<String>(); // ...

Can I solve this with generics types?

Here is the class structure I'd like to have: public class AllocationNEW<TActivity, TResource> where TActivity : AllocatableActivity<TActivity> where TResource : AllocatableResource<TResource> { public TResource Resource { get; private set; } public TActivity Activity { get; private set; } public TimeQuantity Peri...

How to dynamically create generic C# object using reflection?

In C# I have the following object: public class Item { } public class Task<T> { } public class TaskA<T> : Task<T> { } public class TaskB<T> : Task<T> { } I want to dynamically create TaskA or TaskB using C# reflection (Activator.CreateInstance). However I wouldn't know the type before hand, so I need to dynamically create TaskA bas...

Scala: Abstract Types vs Generics

I was reading this, http://www.scala-lang.org/node/105, and wondered when it was better to use Abstract Types e.g. abstract class Buffer { type T val element: T } rather that generics, e.g. abstract class Buffer[T] { val element: T } ...

Java Generics & Hadoop: how to get a class variable

Hi, I'm a .NET programmer doing some Hadoop work in Java and I'm kind of lost here. In Hadoop I am trying to setup a Map-Reduce job where the output key of the Map job is of the type Tuple<IntWritable,Text>. When I set the output key using setOutputKeyclass as follows JobConf conf2 = new JobConf(OutputCounter.class); conf2.setOutputKey...

What is the Collections.checkedList() call for in java?

I just want to know for what java.util.Collections.checkedList() is actually used. I have some code that I know is returning me a List<String> but its being passed through a chain of messaging calls and returned to me as a java.io.Serializable. Is that checkedList call good for me to turn my Serializable into a List<String>? I know...