generics

Instantiating class by generic parameter - which method is better.

I have many html forms referring to many persistence classes. All the html forms are generated by one single class HTMLForm by passing in the respective HTMLFields instances: public class HTMLForm<T>{ HTMLForm(HTMLFields[] f, Class<T> classt){ this.stupidSunWontAllowTnewInstance = classt; // ... whatever GWT jazz .... } public...

Knowing type of generic in Java

Hi all, I have a generic class, says : MyClass<T> Inside a method of this class, I would like to test the type of T, for example : void MyMethod() { if (T == String) ... if (T == int) ... } how can I do that ? Thanks for your help ...

Is it possible to restate these generic constraints in a cleaner way?

Assume following: public class MyFunkyTable : DbObject { // this class will be generated } public class MyFunkyDomainObject : DomainObject { // this class will be custom-made } public class MyFunkyMapper : Mapper<MyFunkyTable, MyFunkyDomainObject> { // this will be custom mapping code due to wired abstraction and ... "supe...

Finding and invoking a generic overloaded method

How can I find a generic overloaded method? For example, Queryable's public static IQueryable<TResult> Select<TSource , TResult> ( this IQueryable<TSource> source , Expression<Func<TSource , int , TResult>> selector ); I've looked for existing solutions, and they're either not generic enough (are based on the method's parameters count...

Converting a generic list to a CSV string

I have a list of integer values (List) and would like to generate a string of comma delimited values. That is all items in the list output to a single comma delimted list. My thoughts... 1. pass the list to a method. 2. Use stringbuilder to iterate the list and append commas 3. Test the last character and if it's a comma, delete it. Wh...

Non-strict multiple interface type parameter constraints?

Excuse me if this is a dupe, but I couldn't seem to get the right combo of keywords to filter down the various type constraint and generics questions out there (as there are a lot). I have two interfaces--let's call them IOnline and IOffline. They're closely related in that they describe nearly identical contracts, but one of the key...

f# casting and generics

Trying to learn some f# and I've run into a couple of hangups. Here's the code: #light module HtmlModule type HtmlString(text:string) = override x.ToString() = text type HtmlAttribute(key:string, value:string) = inherit HtmlString(key + "=\"" + value + "\""); type HtmlElement(tag: string, ?contents:list<'a> when 'a :> HtmlSt...

Test for equality to the default value

The following doesn't compile: public void MyMethod<T>(T value) { if (value == default(T)) { // do stuff } } Error: Operator '==' cannot be applied to operands of type 'T' and 'T' I can't use value == null because T may be a struct. I can't use value.Equals(default(T)) because value may be null. What is the proper...

Basic java question : Type casting

Possible Duplicate: How to cast generic List types in java? When this is feasible Number number = new Integer(""); Why not this ? List<Number> list = new LinkedList<Integer>(); ...

JAXB type Erasure

Hi, I'm trying to create java to xsd for a java type that looks like this. public class MainRequest<E extends AParentType<Person>> extends EObject { private Transaction tran; private E e; private String email; } public class SubRequest extends MainRequest<AChildType> { } where AChildType extends AParentType. when i try to create a s...

Difficulties with Iterator and Generics in Binary Search Tree Implementation

Hey everyone! I am studying Data Structures in java and I am having difficulty with using generics in Binary Search Trees. For our assignment we are to implement a Binary Search Tree using nodes that contain a parent, left and right node as well as a data value. The data value in our case takes the form of a Pair object. This is what i...

How to cast some object to generic class of parent of T?

The main problem of this question is when we pass T into some function and use it to cast some object like the following statement. void SomeFunction<T> (object model) { (SomeClass<T>)model; } Everything works fine. But I want to cast model object to generic class object that is parent of T or grand parent of T depend on what is n...

How to pass a generic object to a Aspx page

I am working on an ASP.Net MVC website and I am stuck on a getting my C# code using generics to play nice with my views. I have a Controller that passes a Model to a View (so far so good). The Model is of type IDescribedEnumerable<T> with some constraints on T, among those is the constraint that T inherits from an interface (IDescribedM...

Genericity vs type-safety? Using void* in C

Coming from OO (C#, Java, Scala) I value very highly the principles of both code reuse and type-safety. Type arguments in the above languages do the job and enable generic data structures which are both type-safe and don't 'waste' code. As I get stuck into C, I'm aware that I have to make a compromise and I'd like it to be the right one...

get type of a generic parameter in java with reflection

Is it possible to get the type of a generic parameter? An example: public final class voodoo { public static chill(List<?> aListWithTypeSpiderMan) { // Here I'd like to get the Class-Object 'SpiderMan' Class typeOfTheList = ???; } public static void main(String... args) { chill(new List<SpiderMan>(...

Designing a wrapper around a WCF web service for using generic CRUD methods - a good solution?

As you all know creating a web service with generic methods is not possible. You have to design the message. But I had the idea of creating a wrapper around WCF using Reflection. public class WcfRepository<T> : IWcfRepository<T> where T : class { public IList<T> GetList() { Type wcfService = typeof ...

C# Generics: Better Way to Match the Generic's Type to Another?

UPDATE: Didn't give a great example. Hopefully it's better now. Is there a better way than this: (typeof(TRepository) == typeof(UserClass)) Here's the use in writing: public static IBaseRepository<TClass> GetRepository<TClass>() where TClass : IDataEntity { IBaseRepository<TClass> repository = null; if (typeof(TClass) == type...

How can I make a generic Clone factory method in C#?

I have the following classes: public abstract class Item { //... } public class Customer : Item { //... } public class Address : Item { //... } I want to be able to create exact clones using a custom reflection class like this: Customer customer = new Customer(); ItemReflector irCustomer = new ItemReflector(custom...

<? super E> and <? extends E> for List

Having the following simple class structure: class A{} class B extends A{} class C extends B{} I'm creating an ArrayList to keep objects of the earlier created classes: List<? extends A> list1 = new ArrayList<A>(); List<? extends B> list2 = new ArrayList<B>(); List<? extends C> list3 = new ArrayList<C>(); List<? super A> list...

Extension method on datacontext Linq to sql

Is it possible to create an extension method on the DataContext, not on the table in the datacontext but directly on the dataContext to get dynamicly a table. ex: DataContext dc = new DataContext(); var test = from a in dc.myExtensionMethod(args) select a; ps: I Already know dc.GetTAble and dc.GetTable<T> ...