generics

Is Predicate<T> not available in .NET 3.0+

Is Predicate available anywhere in .NET? From MSDN http://msdn.microsoft.com/en-us/library/bfcke1bz.aspx, I don't see a Predicate anywhere. I see an anonymous that returns a boolean but no generics or a "Predicate" keyword. ...

Dynamically get the result of a Func<T,bool> invocation

Hi, I am trying to serialize something based upon meeting particular criteria. To this end my original hope was to use attributes containing a lambda expression on an object's properties. However, as this cannot be done I've settled for having a Func<T,bool> member within the class and passing the type (or first parameter type) and na...

Inheritance on a constrained generic type parameter

I know it isn't possible to inherit from a generic type parameter, but it would be handy when implementing a common proxy for derivatives of an abstract type :-) Does anyone know why this isn't possible? Example C#: abstract class Foo { public virtual void Bar() { // nop } } class FooProxy<TFoo> : TFoo where TFoo : Foo {...

Is it possible to emit a type deriving from a generic type while specifying itself as the generic type parameter?

Dear ladies and sirs. Imagine the following perfectly legal type hierarchy: class A<T> where T : A<T> { } class B : A<B> { public B():base(){} } My question is given a statically compiled definition of A<> is it possible to emit the type B dynamically? The problem is how to specify the parent type in ModuleBuilder.DefineType. Or...

How do I return List<String> from a method where I sent to put(Object) into a map (got example)

I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public void set(KEY key, Object value) { values.put(key, value); } private Object getObj(KEY key) { return values.get(key) == null ? key.getDefaultValue() : values.get(key); } public List<E> getList(KEY key) { return (List<E>) getObj(key); } ...

Java as generic higher-order functional programming language

I'm solving algorithmical problems, and i want to write custom functions and predicates that can be applied to collections. Recently, i started using Google Collections and it is just wonderful for this task. I want to use BigInteger and BigDecimal same way, as i would use any other numbers. Without pondering how it should be done, i d...

generic type variable restrictions and interfaces

i'm implementing a generic interface (iqueryprovider, specifically). at some point, i'm forced to return a generic result, that i need to get from some internal interface: public TResult Execute<TResult>(...) { return something.Foo<TResult>(); } where something.Foo is public T Foo<T>() where T: MyBaseClass, new() { ... } this o...

Parse string to enum type

I have an enum type like this as an example: public Enum MyEnum { enum1, enum2, enum3 }; I'll read a string from config file. What I need it to parse the string to MyEnum type or null o not defined. Not sure if the following codes will work (sorry for not having access to my VS right now): // example: ParseEnum<MyEnum>("ENUM1", r...

Class reference from Generic parameter class

I have a persistence framework, and I am trying to use generics so I don't have to keep creating new list classes for each type of object I want to store in a type safe way. I have a method that returns the class of the contained object in the list class (so I know which queries to run and which object to create. As an example, it look...

Convert DataTable to List<>

Hello, I have an strongly typed DataTable 'MyType', I'd like convert it in an List. How can I do this ? Thanks, ...

Can Java methods return type Enum?

I could be wrong but I'm guessing from this previous SO post that, since an enum in Java cannot be declared locally, that therefore it is therefore problematic for a method to return type Enum? I can declare that a method should return an Enum (see below) but how would one then go about implementing such a method to return anything othe...

HtmlHelper<ChildType> not assignable to HtmlHelper<MotherType>

I have a view ViewUserControl<SearchViewData> where SearchViewData: CommonViewData In this view I thus have a reference to Html. This is of the type HtmlHelper<SearchViewData> I create a custom HtmlHelper class called CommonHtmlHelper where I want to this (note the HtmlHelper parameter's type): public static SelectList Translat...

C# Generics - Constraints on type parameters

I'm trying to build a factory method that uses the generics feature of C#. In this factory method I would like to constraint it to some specific classes, all of which do not have a default constructor. Here is my example. Can someone tell me if it's possible to run it? public class AbstractClass { //this abstract class does not h...

Differing behaviour between Java 5 & 6 when overloading generic methods

I've run into an issue in Java's Generics in which the same code will compile and work fine in Java 6, but will fail to compile because of the same erasure in Java 5. I have a file TestErasure.java that has an overloaded method, called "method": import java.util.ArrayList; import java.util.List; public class TestErasure { public stati...

Why Generic Casting is not working on this section of code?

IQueryable<T> IS3Repository.FindAllBuckets<T>() { IQueryable<object> list = _repository.GetAllBuckets().Cast<object>().AsQueryable(); return list == null ? default(T) : (T)list; } This is the error: Error 3 Cannot implicitly convert type 'T' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) I ...

When serializing a custom generic collection to Xml how do I add an attribute to the generated collection element.

When serializing a custom generic collection to Xml how do I add an attribute to the generated collection element. Currently I have: <RootObject> <Id>1</Id> <Items> <MyCollectionItem/> <MyCollectionItem/> </Items> </RootObject> What I need is: <RootObject> <Id>1</Id> <Items Name="My collection name"> <MyCollec...

Fun with Java generics

Anybody knows how to write the piece of code below using generics AND avoiding compiler warnings ? (@SuppressWarnings("unchecked") is considered cheating). And, maybe, checking via generics that the type of "left" is the same as the type of "right" ? public void assertLessOrEqual(Comparable left, Comparable right) { if (left == nul...

Is this a safe and efficient way to handle the reading of null values in a DB using Generics?

I have been playing with generics and was hoping I could get some feedback or suggestions on a function I created to help handle reading null values from the DB. My main concern is in the if statement. Is there a better way to find out if T is a string ect.? Thanks. public static T CheckNull<T>(object value) { if ((value ...

How to make a generic MethodInvoke ???

Context I want to make this call via reflection instanceOfEventPublisher.Publish<T>(T eventInst); When I call ` private void GenCall(IEventPublisher eventPublisher, object theEventObj){ var thePublisher = eventPublisher.GetType(); thePublisher.InvokeMember( "Publish", BindingFlags.Default | B...

How do i get Intellisense for <T> class?

i'm creating an html.helper for a 3rd party javascript grid component. i'm passing my gridextension my viewmodel. in my viewmodel class i've got custom attributes on my properties describing how each column is displayed. in my gridextension, i want to then serialize my class of T. in my CreateSerializedRow method i'd like to be a...