generics

Generics on ASP.NET page Class

I want to implement Generics in my Page Class like : Public Class MyClass(Of TheClass) Inherits System.Web.UI.Page But for this to work, I need to be able to instantiate the Class (with the correct Generic Class Type) and load the page, instead of a regular Response.Redirect. Is there a way to do this ? ...

How do I determine the value of a generic parameter on my class instance

I have a marker interface defined as public interface IExtender<T> { } I have a class that implements IExtender public class UserExtender : IExtender<User> At runtime I recieve the UserExtender type as a parameter to my evaluating method public Type Evaluate(Type type) // type == typeof(UserExtender) How do I make my Evaluate me...

How to avoid type safety warnings with Hibernate HQL results?

For example I have such query: Query q = sess.createQuery("from Cat cat"); List cats = q.list(); If I try to make something like this it will show warning "Type safety: The expression of type List needs unchecked conversion to conform to List": List<Cat> cats = q.list(); Is there a way to avoid it? Thanks. ...

Is there an "anonymous" generic tag in C#, like '?' in Java?

In Java, one can declare a variable parameterised by an "unknown" generic type, which looks like this: Foo<?> x; Is there an equivalent construct to this question-mark, in C#? ...

Do C# Generics Have a Perfomance Benefit?

I have a number of data classes representing various entities. Which is better: writing a generic class ( to say, print or output XML) using generics and interfaces, or writing a seperate class to deal with each data class? Is there a performance benefit/ or any other benefit (other than it saving me the time of writing separate class...

How do I implement IEqualityComparer on an immutable generic Pair struct?

Currently I have this (edited after reading advice): struct Pair<T, K> : IEqualityComparer<Pair<T, K>> { readonly private T _first; readonly private K _second; public Pair(T first, K second) { _first = first; _second = second; } public T First { get { return _first; } } public K Second { ge...

.NET Generic Method Question

I'm trying to grasp the concept of .NET Generics and actually use them in my own code but I keep running into a problem. Can someone try to explain to me why the following setup does not compile? public class ClassA { ClassB b = new ClassB(); public void MethodA<T>(IRepo<T> repo) where T : ITypeEntity { b.MethodB(repo); } } pub...

.NET EventHandlers - Generic or no?

Every time I start in deep in a C# project, I end up with lots of events that really just need to pass a single item. I stick with the EventHandler/EventArgs practice, but what I like to do is have something like: public delegate void EventHandler<T>(object src, EventArgs<T> args); public class EventArgs<T>: EventArgs { private T i...

What's the best way to instantiate a generic from its name?

Assuming I have only the class name of a generic as a string in the form of "MyCustomGenericCollection(of MyCustomObjectClass)" and don't know the assembly it comes from, what is the easiest way to create an instance of that object? If it helps, I know that the class implements IMyCustomInterface and is from an assembly loaded into the...

Double generic constraint on class in Java: extends ConcreteClass & I

Is there a way to define a generic constraint in Java which would be analogous to the following C# generic constratint ? class Class1<I,T> where I : Interface1, Class2 : I I'm trying to do it like this: class Class1<I extends Interface1, T extands I & Class2> But the compiler complains about the "Class2" part: Type parameter cannot...

Creating a multi-dimensional hashtable with generics in VB ASP.NET?

I need to create a multi-dimensional (nested) hashtable/dictionary so that I can use syntax like val = myHash("Key").("key") I know I need to use Generics but I can't figure out the correct syntax using VB in ASP.NET 2.0, there are plenty of c# examples on the net but they aren't helping much. Cheers! ...

Is it possible to implement a COM interface with a .NET generics class?

I have the following interface which I'm trying to make COM-visible. When I try to generate the type-library it doesn't like the fact that my implementation class derives from a generic-class. Is it possible to use a generic class as a COM implementation class? (I know I could write a non-generic wrapper and export that to COM, but th...

Generic Object

I have a class declared as follows: Public MustInherit Container(Of T As {New, BaseClass}) Inherits ArrayList(Of T) I have classes that inherit this class. I have another class that I must pass instances in this method: Public Sub LoadCollection(Of T As {BaseClass, New})(ByRef Collection As Container(Of T)) I need to store the pas...

Can I use a List<T> as a collection of method pointers? (C#)

I want to create a list of methods to execute. Each method has the same signature. I thought about putting delegates in a generic collection, but I keep getting this error: 'method' is a 'variable' but is used like a 'method' In theory, here is what I would like to do: List<object> methodsToExecute; int Add(int x, int y) { retur...

Redundancy in C#?

Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by the language designers? If so, why? ...

What is the best way to clone/deep copy a .NET generic Dictionary<string, T>?

I've got a generic dictionary Dictionary that I would like to essentially make a Clone() of ..any suggestions. ...

Do generic interfaces in C# prevent boxing? (.NET vs Mono performance)

I have a C# interface with certain method parameters declared as object types. However, the actual type passed around can differ depending on the class implementing the interface: public interface IMyInterface { void MyMethod(object arg); } public class MyClass1 : IMyInterface { public void MyMethod(object arg) { My...

Is there a way to determine the signature of a Lua function?

Recently, Lee Baldwin showed how to write a generic, variable argument memoize function. I thought it would be better to return a simpler function where only one parameter is required. Here is my total bogus attempt: local function memoize(f) local cache = {} if select('#', ...) == 1 then return function (x) ...

implicit operator using interfaces

I have a generic class that I'm trying to implement implicit type casting for. While it mostly works, it won't work for interface casting. Upon further investigation, I found that there is a compiler error: "User-defined conversion from interface" that applies. While I understand that this should be enforced in some cases, what I'm tr...

Specifying the type of ArrayList's elements

I thought that there was some way in .net 3.0 to give an array list a type so that it didnt just return Object's but I'm having trouble doing so. Is it possible? If so, how? ...