generics

Clever caching using generics?

Are there any clever layers out there to sit on top of the System.Web.Caching.Cache class to handle simple scenarios to fetch items using generics. I'd like to maybe provide a delegate function to retrieve an item, and be able to fetch an item with a strongly typed GetItem and a delegate function to retrieve an item if it isnt found. F...

Supporting covariant type conversions in Java

The Java type system supports only invariant types. So a List<String> is not an List<Object>. A List<String> is not a List<Object> as it is not valid to insert an Integer into a List<String>. However, there are types for which such a covariant type conversion is valid. Given the classes A, B and Producer: class A{} class B{} interface ...

"using the generic type ....<T> requires 1 type argument and generic multiple constraints and using either

I have an interface as follows: public interface ITestCase<T> where T : HtmlControl { IEnumerable<T> Execute(Manager manager); } HtmlControl is a base class from a 3rd party assembly. I have two implementations of this interface. One of them is returning null (specified by me), until I nail the logic required (not a difficult ta...

Most succinct way to convert ListBox.items to a generic list

I am using C# and targeting the .NET Framework 3.5. I'm looking for a small, succinct and efficient piece of code to copy all of the items in a ListBox to a List<String> (Generic List). At the moment I have something similar to the below code: List<String> myOtherList = new List<String>(); // Populate our colCriteria ...

Writing Unittest for generic classes... best approach?

Hello! I'm supposed to write unit tests for a project whose code another colleague has written. He created a generic list class. Now i'm thinking about, the best way to create the unittests for the class and how to avoid much repetitive code. The List class can be instaniated with String, Integer and some other Types. How can i avoid to ...

Setting List<? extends Interface1>

Trying to do something like: public interface Order { public List<? extends OrderItem> getItems(); public void setItems(List<? extends OrderItem> items); } public interface OrderItem { // stuff } public calss OrderItemImp implements OrderItem { // stuff } public class OrderImp implements Order { ...

Java: Instanceof and Generics

Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this: @Override public int indexOf(Object arg0) { if (!(arg0 instanceof E)) { return -1; } What is the better way to do it? This is the error mes...

Why can't I do this with implicit types in C#?

var x = new { a = "foobar", b = 42 }; List<x.GetType()> y; Is there a different way to do what I want to do here? If there's not, I don't really see all that much point in implicit types... ...

Java: Retrieve the generic parameter value at runtime

What is the best way to retrieve the runtime value of a generic parameter for a generic class? For example: public class MyClass<T> { public void printT() { // print the class of T, something like: // System.out.println(T.class.getName()); } } So if I call new MyClass<String>().printT() it will print "String" ...

How would I know if a property is a generic collection

I need to know if the type of a property in a class is a generic collection (List, ObservableCollection) using the PropertyInfo class. enter code here foreach (PropertyInfo p in (o.GetType()).GetProperties()) { if(p is Collection<T> ????? ) } ...

C# Generic question

I have this function public DataSet Fetch(string EntityName, ObjectParameter[] parameters, int pagesize, int pageindex) { Assembly asm = Assembly.Load("NCR.WO.PLU.ItemEDM"); Type _type = asm.GetTypes().Where(t => t.Name.Equals(EntityName)).ToList().FirstOrDefault(); object obj = Activator.CreateInstance(_typ...

How can I enforce a type constructor in a concrete class through the interface it implements?

Let me describe the situation, and I'm sure I'm just thinking about this problem incorrectly. I have a concrete class that will implement an interface. I want to enforce in the contract that the class must have a constructor with a specific type. So for instance: interface MyInterface {} public class MyClass implements MyInterface { ...

How to write a generic class that implements an interface having generic methods with constraints defined

I'm new to Generics implementation and need inputs on 2 issues I face : I have an interface ICommand defined as : public ICommand { List<T> Execute<T>() where T : IValidationResult; IDomain<T> GetDO<T>() where T : IValidationResult; } intentionally I have it as non-generic as I have to add a collection of different commands. ...

How to specify a generic type as a restriction in a generic method

I have a generic class like this: class MyGenericClass<T> { } On the other hand I have an aggregator class that I use to obtain singleton instances of MyGenericClass and of derived classes, where each instance is identified by the concrete class type and by the parameter type. That is, MyGenericClass<int> is different from MyGenericCl...

Why can I not compare two generic objects of a certain implicit type?

Consider the following code: public class SystemManager<T> where T : ISettings { public SystemManager() { T implicit1 = default(T); T implicit2 = default(T); if (implicit1 != implicit2) { // This will not compile saying the compiler cannot compare // using '!=' two objects ...

Why can't I catch a generic exception in C#?

I was doing some unit testing on code that could throw a number of exceptions depending on the inputs. So I tried something like the below code: (simplified for the example) static void Main(string[] args) { RunTest<ArgumentException>(); } static void RunTest<T>() where T : Exception, new() { try ...

Passing a single item as IEnumerable<T>

Is there a common way to pass a single item of type T to a method which expects an IEnumerable<T> parameter? Language is C#, framework version 2.0. Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting helper methods similar to LINQ), but this just seems silly: public static class IEnumerab...

In a knot with Generics

I have the following domain object: public class DomainObject<T,TRepo> where T : DomainObject<T> where TRepo : IRepository<T> { public static TRepo Repository { get;private set; } } A repository interface: public interface IRepository<T> //where T : DomainObject<T> // The catch 22 { void Save(T domainObject); } An im...

Is it possible to instantiate an object of generic type in Java or C# ?

For example, in Java, if I have a parameterized class, is it possible to do the following? public class MyClass<T>{ public void aMethod { .. T object = new T(); .. } .. } In Java, I think it is not possible, because the compiler doesn't know what constructor to call. But in C#? I don'...

Is this code sample really returning the right parameterized types?

I've read the Beautiful code with Google Collections, Guava and static imports article about Java collections, and the following snippet got my attention: Map<String, Map<Long, List<String>>> map = Maps.newHashMap(); The thing is, I don't understand how it's possible that the newHashMap method can return a Map<String,Map<Long, List<St...