generics

Writing a generic API for a type 'family'

I'm working with a smallish type hierarchy, something like the following, and lets say there won't ever be any other Animal types in my sad safari-less world (I'm not at all worried about resilience to expanding): public abstract class Animal {}; public sealed class Dog : Animal {}; public sealed class Cat : Animal {}; public sealed cla...

C# - Use OfType and ignore inherited classes

I have a MenuStrip with lots of items and am trying to have on event they all subscribe to so I am trying to do menuStrip1.Items.OfType<ToolStripMenuItem>(); and for each one I do this: menuitem.Click += new EventHandler(MenuItem_Click); The problem is there is a ToolStripSeperatorItem which inherits off ToolStripMenuItem and that app...

C# Generics and Reflection

Hi, i'm using linq. All my queries looks like var query = dc.GetTable<myType>(). I wish i could choose "myType" using a string parameter. I tried to create a Type object using reflection, but the compiler doesn't recognize Type objects as class definitions. Any suggestions? Thanks ...

Use generics to make a method work for any data type

I'm not quite sure I'm understanding how I can utilize generics in C# properly. Say I have the following method. I would like to allow it to work on Lists of any type. Currently I have List where Row is a custom struct, I want to reuse this sort method for half a dozen structs that I make. I thought I could just do List<T> in the return ...

Relationship between instances of List<T>

Is there any way to tell via reflection that a generic list of Type A is related to a generic list of Type B? For example, I have a List<string> and a List<int>. How can I tell via reflection that both these types are 'instances' of List<T>. I think I'm having a problem because List<T> isn't a real type. You can't do typeof(List<T>) ...

Casting method arguments of a Generic Class...

I've made a generic class that saves and queries objects from a flat file. I keep having to change the method arguments to objects so I can cast them and I'm wondering if I'm going about this the right way... 'T' will always inherit 'FlatFileRecord' This does not compile: public class FlatFile<T> { public void Save(T record) { ...

Can I declare a variable of Type<T> without specifying T at compile time?

Possible Duplicate: Can I declare a variable of Type<T> without specifying T at compile time? Objective: To Load the class "MyContent" dynamically. I have 1 interface<T>, 1 abstract generic class<T>. Code: public interface IMyObjectInterface{ } public abstract MyAbstractObject : IMyObjectInterface{ } public class MyObject : My...

How to declare field-level type parameters?

In the following code sniplet, I want to specify that: attachment and handler share a generic type ` <A>'s type only needs to be specified when notify() is invoked Invoking notify() is optional. I do not want to force users to specify <A> at class construction time because they might never end up invoking notify(). /** * Builder pa...

Java generics problem

I'm implementing a high-order split function, which takes a collection of items to be splitted, a filter delegate which defines the boundary criteria of the split, and two ReturnDelegate, which are delegates that provides the concrete collection type of the return collections (so I don't need to fix ArrayList or HashSet in the split func...

C# Dynamic Dictionary...

I would like to find a way to have a Dictionary of various Mock objects with their various instances keyed by some kind of unique identifier. I am using Moq, so the syntax for that is Mock<IFoo> as far as the typing goes. But I would like to have the Dictionary contain basically this so I can dynamically resolve and overwrite instances a...

Is it possible to create a collection of generic contrained types in .net?

Is something like this possible? Dim l As New List(Of T As Type Where GetType(BaseClass).IsAssignableFrom(T)) Note, my collection will be a collection of Types, not objects of type T - which I know is possible. ETA: The answers I've had so far are as expected - I didn't think it was possible. What I'm trying to get my head round is...

Loop through System.Collections.Generic.Dictionary via for statement

Hi folks, I have a quick question. Is there way to easy loop through System.Collections.Generic.Dictionary via for statement in C#? Thanks in advance. ...

C# Generics Problem

Hi, I have a problem dealing with Generics. Some background: The Animal class is a generified class that needs to be associated with a particular person Dog is an animal that can only be associated with the class Doctor (a subclass of Person) Cat is an animal that can only be associated with the class Painter (a subclass of Person...

Can I pass a type object to a generic method?

Hi, I have a FindAll method on my DataAccessLayer which looks like this: public FindResult<T> FindAll<T>() where T : Entity, new() and a client code that has a Type[] array which it needs to use to iteratively call the FindAll method with like this: foreach (var type in typeArray) { var result = DataAccessLayer.FindAll<type>...

Reflection - check all nullable properties have values

I have to loop through all the properties in a few classes and check any nullable properties to see if they have a value. How do I cast the value returned from propertyInfo.GetValue() to a generic nullable type so that I can check the HasValue property? Code snipped for brevity: foreach (PropertyInfo propInfo in this.GetType().GetProp...

reflection java ? extends

Is there any difference between : Class<?> and Class<? extends Object> ? Thanks ...

Generic View-Model Editing Support

I have an abstract generic view-model that I use as a base-class for several other view-models. It is defined as follows: public abstract class DiscreteViewModel<T> { protected DiscreteItem<T> _selectedItem; ... } My DiscreteItem class is also generic, and as follows: public class DiscreteItem<T> { public T Display { get;...

C# Generics, Comparing 2 strings fail unless explicitly specified

I thought i've seen it all but this... :) I was working on a generic graph of type string, Graph<string> graph = new Graph<string>(); Graph is declared with a class constraint like this: public class Graph<T> where T : class Next i fill up the graph with some dynamicly generated strings: for (char t = 'A'; t < 'J'; t++) { Gra...

Java: Instantiating a generic class with no default constructor

Hi, I am trying to do this: public class BaseTable<T extends TableEntry> { protected int mRows; protected int mCols; protected ArrayList<T> mEntries; public BaseTable(int rows, int cols) { mRows = rows; mCols = cols; mEntries = new ArrayList<T>(); for (int i = 0; i < rows; i++) ...

.NET: Inferred generic types on static methods

Suppose I have public static List<T2> Map<T,T2>(List<T> inputs, Func<T, T2> f) { return inputs.ConvertAll((x) => f(x)); } private int Square(int x) { return x*x; } public void Run() { var inputs = new List<Int32>(new int[]{2,4,8,16,32,64,128,256,512,1024,2048}); // this does not compile var outputs = Map(inputs, Squa...