generics

create a generic method that sorts List<T> collections

What I used to do is create a case switch that sorts my LINQ in this format: List<products> productList = GetAllLists(); switch (sortBy) { case "name": return productsList.OrderBy(pl => pl.Name); case "date": return productsList.OrderBy(pl => pl.DateCreate); } which, in the long run, becomes cumbersome. I wanted to hav...

How to get the <?> value for a Foo<?> object?

(This is somewhat a followup to my previous question) I've got a Foo<?> object, foo. Foo<T> is an interface. How to get the type value hidden behind the <?>? Note that this is not trivial, as foo can be for example an object of class Bar<String>, where Bar<T> implements Foo<T>, or some anonyomus class implementing interface FloatFoo, ...

Apply operations on a one-dimensional array of instances in one line, using LINQ

Consider the following structures: internal struct Coordinate { public Double Top { get; set; } public Double Left { get; set; } } internal struct Dimension { public Double Height { get; set; } public Double Width { get; set; } } internal struct Property { public Boolean Visible { get; set; } internal String La...

Java Convert Generic LinkedList to Generic Array

So for starters lets say that I have a LinkedList<String>, I can easily convert it to an array via toArray(). i.e. LinkedList<String> strList = new LinkedList<String>(); String[] strArray = strList.toArray(new String[0]); But Lets say I have a LinkedList<T> Then I the following code: LinkedList<T> tList = new LinkedList<T>(); T[] s...

F# Generic Programming - Using members

Suppose I have a family of types which support a given member function, like the Property member below: type FooA = {...} with member this.Property = ... type FooB = {...} with member this.Property = ... Suppose the member Property returns an integer for each of the the above types. Now, I wan to write a generic function th...

What are some examples of dynamic/generic/other methods for generating return type based on caller's type ?

In the context of C#, .Net 4... Given a data source object that supplies vertices by index from an array of doubles where a vertex contains ten doubles with members Px, Py, Pz, Nx, Ny, Nz, S, T, U, V. and the backing array contains all or any subset of vertex members based on the data source's stride, offset and count properties. The da...

Casting a list of lists to IGrouping?

I have some code that creates a list of lists. The list is of this type: List<List<Device>> GroupedDeviceList = new List<List<Device>>(); But need to return the result in the following type: IEnumerable<IGrouping<object, Device>> Is this possible via a cast etc or should I be using a different definition for my list of lists? ...

C#: How to create a generic superclass to be extended by non-generic subclasses

I have a bunch of Repository classes which all look a bit like the following. Note that I have omitted certain methods; I just want you to get a flavour. public class SuggestionRepository : ISuggestionRepository { private IUnitOfWork _unitOfWork; public SuggestionRepository(IUnitOfWork unitOfWork) { _unitOfWork = un...

How to use Inheritance when using Generic Constraints

I'm struggling with some Generic constraint issues when trying to implement a library that allows inheritance and hoping someone can help. I'm trying to build up a class library that has 3 flavours to it, each building on top of the other. To me it seemed like a perfect opportunity to use Generics as I can't quite do what I want throug...

C#: Generic implementation of method doesn't satisfy interface.

In this post I talked about using a generic base class to enable me to create repository classes without duplicating loads of basic plumbing code. Each Repository is accessed through an interface. In the code below, I will only show one of the methods for the sake of brevity: Interface: IQueryable<Suggestion> All { get; } Generic ba...

How to decide at runtime which type to pass as a generic parameter?

I have several message queues that have specific messages on them. I've created classes for these messages using xsd.exe. I can receive a message syncronously and deseriazlise it with this method: public oneOfMyTypes DeserializeMessage(XDocument message) { var serializer = new XmlSerializer(typeof(oneOfMyTypes)); var entity = (oneOfMy...

Issue understanding the distinction between Methods, Generic Types etc...

In the code below the "Move" public class derives fromthe generic type "Submit". "Submit" is a method, part of the DSS model, which handles messages and accepts two parameters, one is the message body and one is the message response. My question is: How or WHY does a class derive from a method?! It seems to me (since i'm only a beginne...

WPF dependency properties - set in XAML when base class is generic

Hi As per the title really, how can you set a dependency property in XAML when the base class is generic? When trying to do this I get a NullReferenceException, setting the property from code behind works fine. It also works when the base class is not generic. I'm using .NET4 Here is some sample code to demonstrate: WindowBase.cs usi...

Generic extension method : Type argument cannot be inferred from the usage

I'm trying to create a generic extension method, that works on typed data tables : public static class Extensions { public static TableType DoSomething<TableType, RowType>(this TableType table, param Expression<Func<RowType, bool>>[] predicates) where TableType : TypedTableBase<RowType> where RowType : DataRow { ...

java class declaration <T>

hi all it's simple class declaration public class test but i don't understand it public class test<T> . ...

C# generics constraint: must define an overloaded operator

Possible Duplicates: Solution for overloaded operator constraint in .NET generics Define a generic that implements the + operator Hi, So I have a generic class MyClass<T> and I want to put a constraint that the type T must define an operator, for example public static T operator +(T x,T y). Is that possible? If not, I was...

Comparison of a generic type with it's default value, without a generic class constraint, gives a compile time error.

I just ran into this situation and i thought it was a nice opportunity to use the default keyword. But it doesn't compile and i can't think of why. The example below illustrates my problem: public class Test<TDataSource> { public IQueryable<TDataSource> DataSource { get; set; } public bool GetOneOrDefaultResult() { ...

Create a generic list using reflection

I have a function that uses reflection to set properties of object A from object B. At one point, I need to instantiate a generic collection. However, I am unable to get it working. Here is what I have now: IList list = destProperty.PropertyType.GetGenericTypeDefinition() .MakeGenericType(destProperty.PropertyType.GetGen...

Implementing Addition in Inheriting Classes with Generics.

With the structure.. abstract class Unit { int Id; } class Measure : Unit { int Current; int Baseline; } class Weight : Unit { int Minimum; int Maximum; int Current; } I basically want to add an "Add" method for adding, say, two Measures together, or adding two Weights together. But it needs to be in the Unit base class. So ba...

C# async callback still on background thread...help! (preferably without InvokeRequired)

Hi there, I am writing a very simple asynchronous helper class to go along with my project. The purpose of the class is that it allows a method to be run on a background thread. Here is the code; internal class AsyncHelper { private readonly Stopwatch timer = new Stopwatch(); internal event DownloadCompleteHa...