generics

How do I sort a generic list?

I have a generic list... public List<ApprovalEventDto> ApprovalEvents The ApprovalEventDto has public class ApprovalEventDto { public string Event { get; set; } public DateTime EventDate { get; set; } } How do I sort the list by the event date? ...

(C#) Get random data using Generics

One of our unit tests is to populate properties within our business objects with random data. These properties are of different intrinsic types and therefore we would like to use the power of generics to return data of the type you pass in. Something along the lines of: public static T GetData<T>() How would you go about approaching ...

ServiceProvider, cache etc. done with generics without cast

I'm talking about c# 3.5 3.0. I know how to do it when cache or ServiceProvider can have only one instance for the whole application. In this case ServiceProvider can look like this public static class Service<T> { public static T Value {get; set;} } and can be used for different types like this: Service<IDbConnection>.Value = ne...

Create Generic List from a subclass

Option Explicit On Option Strict On Public Class Class1 Dim l As List(Of A) Public Sub New() l = New List(Of B) End Sub End Class Public Class A End Class Public Class B Inherits A End Class<p> I've run into this problem. I have a list declared of a Generic Type 'A' I want to define the list as a Generic T...

How to determine the class of a generic type?

Hi all, I'm creating a generic class and in one of the methods I need to know the Class of the generic type currently in use. The reason is that one of the method's I call expects this as an argument. Example: public class MyGenericClass<T> { public void doSomething() { // Snip... // Call to a 3rd party lib T bean = (T)s...

How to test whether method return type matches List<String>

What is the easiest way to test (using reflection), whether given method (i.e. java.lang.Method instance) has a return type, which can be safely casted to List<String>? Consider this snippet: public static class StringList extends ArrayList<String> {} public List<String> method1(); public ArrayList<String> method2(); public StringList...

Compiler fails converting a constrained generic type.

Hello, I have a class that has a Generic type "G" In my class model i have public class DetailElement : ElementDefinition Let's say i have a method like this public void DoSomething<G>(G generic) where G : ElementDefinition { if (generic is DetailElement) { ((Detai...

Can I specify a generic type in XAML?

In XAML I can declare a DataTemplate so that the template is used whenever a specific type is displayed. For example, this DataTemplate will use a TextBlock to display the name of a customer: <DataTemplate DataType="{x:Type my:Customer}"> <TextBlock Text="{Binding Name}" /> </DataTemplate> I'm wondering if it's possible to define ...

Java Generics Syntax for arrays

What data structure does the following declaration specify? List<ArrayList>[] myArray; I think it should declare an array where each element is a List (e.g., a LinkedList or an ArrayList) and require that each List contain ArrayList objects. My reasoning: List<String> someList; // A List of String objects List<ArrayLi...

Are there anonymous, type-safe, generic delegate signatures in C# 2.0?

Consider the delegate for a generic A to B function: public delegate B Fun<A, B>(A x); I can then write a function that accepts and invokes the Fun delegate: public static B invokeFun<A, B>(A x, Fun<A, B> f) { return f(x); } (Never mind whether it is wise to write invokeFun.) Can I write invokeFun without naming the Fun delegate? ...

C# List<> OrderBy Alphabetical Order

I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<>. For the sake of this example lets say I have a List of a Person type with a property of lastname. How would I sort this List using a lambda expression? List<Person> people = PopulateList(); people.OrderBy(???? => ?????) ...

Is there a way to cast generic lists to lists of interface/base class types?

I'm trying to show someone a use for interfaces in a crazy situation they've created. They have several unrelated objects in lists, and need to perform an operation on two string properties in each object. I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a me...

VB.NET generics to C# syntax

Could you please show me the C# Equivalent of this VB.NET code: Public Partial Class Index Inherits System.Web.Mvc.Viewpage(Of List(Of Task)) End Class I am not sure where/how to add it in for C#: public partial class DirList : System.Web.Mvc.ViewPage { } The code is suppose to tell the class to expect a list of tasks from t...

VB.NET Strong-typed collection

I want to create a collection in VB.NET, but I only want it to accept objects of a certain type. For example, I want to create a class called "FooCollection" that acts like a collection in every way, but only accepts objects of type "Foo". I thought I could do this using generics, using the following syntax: Public Class FooCollect...

C# Generics wont allow Delegate Type Constraints

Is it possible to define a class in C# such that class GenericCollection<T> : SomeBaseCollection<T> where T : Delegate I couldn't for the life of me accomplish this last night in .NET 3.5. I tried using delegate, Delegate, Action<T> and Func<T, T> It seems to me that this should be allowable in some way. I'm trying to implement my o...

Calling a static method on a generic type parameter

I was hoping to do something like this, but it appears to be illegal in C#: public Collection MethodThatFetchesSomething<T>() where T : SomeBaseClass { return T.StaticMethodOnSomeBaseClassThatReturnsCollection(); } I get a compile-time error: "'T' is a 'type parameter', which is not valid in the given context." Given a gener...

Reflection and generic types

I'm writing some code for a class constructor which loops through all the properties of the class and calls a generic static method which populates my class with data from an external API. So I've got this as an example class: public class MyClass{ public string Property1 { get; set; } public int Property2 { get; set; } public boo...

Convert dictionary values into array

what is the most efficient way of turning the list of values of a dictionary into an array for example, if i have: Dictionary where key is string and value is Foo i want to get Foo[] I am using VS 2005, C# 2.0 ...

Why can't I use a type argument in a type parameter with multiple bounds?

So, I understand that the following doesn't work, but why doesn't it work? interface Adapter<E> {} class Adaptulator<I> { <E, A extends I E>> void add(Class<E> extl, Class<A> intl) { addAdapterFactory(new AdapterFactory<E, A>(extl, intl)); } } The add() method gives me a compile error, "Cannot specify any additional bou...

Using Castle Windsor's fluent interface to register components in a decorator chain, when there are also specialised service-types?

I am trying to implement a decorator chain for my data-access based on IRepository. I have a Repository that does the data- access (at the moment just in-memory list) {1}, and I have one that does validation {2}. On top of that, I have a specialisation of my Repository, IUsersRepository {3}, which defines another method on the base int...