generics

Does java implement type erasure in user defined generic classes too?

Suppose I have a class and I want to use it somewhere as a generic type: class MyList<T> { T[] list=T[10]; public void add(T element) { list[0]=element; } } After compilation, does it remove its type information like it is the case for generic collections? I don't need to use this code anywhere, so please do...

How can I use generics to put this method in the parent class?

I have a number of plural item classes which each have a collection of singular item classes, like this: public class Contracts : Items { public List<Contract> _collection = new List<Contract>(); public List<Contract> Collection { get { return _collection; } ...

How do I enumerate all items that implement a generic interface?

I have two interfaces, a generic and a non-generic that have an inheritence hierarchy: public interface IGenericRelation<TParent, TChild> : IRelation public interface IRelation The generic one is implemented by several server controls that are loaded dynamically and I wish to enumerate on the collection of controls that implement th...

Java generics - why is "extends T" allowed but not "implements T"?

Hi folks, I wonder if there is a special reason in Java for using always "extends" rather than "implements" for defining bounds of typeparameters. Example: public interface C {} public class A<B implements C>{} is prohibited but public class A<B extends C>{} is correct. What is the reason for that? ...

How to specify a WCF known type in config that is generic?

I have a type, let's call it Data<TKey>. I also have a WCF service contract that accepts a type (lets call it Wrapper) with a property of type Object (for reasons I won't go into, this isn't optional). [DataContract] public class Data<TKey> { ... } [DataContract] public class Wrapper { [DataMember] public object DataItem { get...

c#: generically convert unmanaged array to managed list

I am dealing with a set of native functions that return data through dynamically-allocated arrays. The functions take a reference pointer as input, then point it to the resulting array. For example: typedef struct result { //..Some Members..// } int extern WINAPI getInfo(result**); After the call, 'result' points to a null-termi...

Multiple where for generic type

I need to specify that a generic type for my class implements an interface, and is also a reference type. I tried both the code snippets below but neither work public abstract class MyClass<TMyType> where TMyType : IMyInterface where TMyType : class public abstract class MyClass<TMyType> where TMyType : class, IMyInterfa...

What is the syntax to use C# generics as a constructor name?

I've got a number of classes that inherit from Item<T>. Each class has a Create() method that I would like to move up into Item<T>. Yet the following code gets the error "Cannot create an instance of the variable type 'T' because it does not have the new() constraint": T item = new T(loadCode); What is the correction syntax to do th...

C#: No implict conversion from Class<Child> to Class<Base>

Following snippet wouldn't compile. With following error: Cannot implicitly convert type 'Container<ChildClass>' to 'Container<BaseClass>' class BaseClass {} class ChildClass : BaseClass {} class Container<T> where T : BaseClass {} class Program { static void Main() { // why doesn't this work? Container<BaseClas...

C# get the types defining a Dictionary at run time

Hi there, I was wondering what is the best way for getting the generic arguments that definine a dictionary at run time is. Take for example: Dictionary<string, object> dict; How at runtime can I find out that the keys are strings? Thanks ...

Testing if object is of generic type in C#

I would like to perform a test if an object is of a generic type. I've tried the following without success: public bool Test() { List<int> list = new List<int>(); return list.GetType() == typeof(List<>); } What am I doing wrong and how do I perform this test? ...

different return type from method in generic class

The following code is just made up, is this possible to do with C#? class A { public int DoStuff() { return 0; } } class B { public string DoStuff() { return ""; } } class MyMagicGenericContainer<T> where T : A, B { //Below is the magic <------------------------------------------------ ...

C# Generics and Type Checking

I have a method that uses an IList as a parameter. I need to check what the type of that T object is and do something based on it. I was trying to use the T value, but the compiler does not not allow it. My solution is the following: private static string BuildClause<T>(IList<T> clause) { if (clause.Count > 0) { if (c...

VB.Net Iniatialising a class using System.Reflection and System.Type to create a session based singlton extension method.

I have had several occasions recently to access a specific class several times over a relatively small time frame. So I've been storing the value of the class in Session and trying to access it on page load, if it's not available creating a new instance and storing that in session. So instead of constantly replicating the same code for...

Differentiating between generic and non-generic version of overloaded method using reflection

I'm having some trouble using reflection to differentiate between a non-generic and a generic method on a generic class. Here's a test case I'm working with: public class Foo<T> { public string Bar( T value ) { return "Called Bar(T)"; } public string Bar( int value ) { return "Called Bar(int)"; } public static void CallBar<TR>(F...

Generic Linked List for Delphi 2009

I was looking in Generics.Collections and noticed there was no linked list. Sure they are simple to make, but I thought it was odd there was not one (or I just missed it). Are linked lists just outdated when compared to new modern data structures, or is there a need for a general generic linked list? Does anyone know of one? ...

Is it possible to create a common database class in VB.NET?

We have 3 databases providers we use to connect to our databases: DB2, MS SQL, and Interbase. I would like to create a single generic database wrapper class that can be used to talk to all three just by passing in the correct connection string, username, password, and the provider desired. I don't want to have to add references and imp...

C# Generics and Casting Issue

I am having trouble casting an object to a generic IList. I have a group of in statements to try to work around this, but there has to be a better way to do this. This is my current method: string values; if (colFilter.Value is IList<int>) { values = BuildClause((IList<int>)colFilter.Value, prefix); } else if (colFilter.Value is ...

List<T>.Contains not showing overload

I'm sure that I'm missing something simple here, but in Visual Studio 2008 on my current project only List.Contains(T) is present. Being able to use List.Contains(T, IEqualityComparer(T)) would save me a lot of grief. Any quick thoughts on this? Thanks. *edit: using System.Linq is not available to me. Is is possible that my project is ...

Problem with Generic Inheritance (Open Construction)

Hi, I am facing this strange problem while working with Generic Base Classes. I am having three levels of base class hierarchy while the fourth level is the concrete class. Something as below. // Level 0 (Root Base Class) public abstract class BusinessDataControllerBase<BDA> : IBusinessDataController { protected BDA da; publ...