generics

Why are the names of generic types mangled in a .NET stack trace?

I have an exception being thrown from a C# method, that takes a generic list as a paremeter. private static void DoWork(List<ClassName> a) { } When it throws an exception, the stack trace shows an `1 instead of the class name for the list. Why is this? This is what the stack trace has. ... at DoWork(List`1 a). ... ...

Get the actual type of a generic object parameter

Hi, No doubt elements of this question have been asked before, but I'm having trouble finding an answer. (Disclaimer: this is related, but separate from a recent question I asked). I have a method like this: public static void Method<T>(MethodInfo m, T value) { Type memberType = m.GetValueType(); if (memberType.IsAssignableFrom(...

Build c# Generic Type defintion at runtime.

At present I'm having to do something like this to build a Type definition at runtime to pass to my IOC to resolve. Simplified: Type t = Type.GetType( "System.Collections.Generic.List`1[[ConsoleApplication2.Program+Person"); I know the generic type argument at runtime only. Is there something that will allow me to do something like ...

How to obtain class instance generic argument type

In code, if i write a code line like: gClass<Double> x = new gClass<Double>(); And let say, that class is like: public static class gClass<T> { private T value = null; public gClass() { // What is T? } } Where i inserted the question: "What is T?", i don't want the value (what is null), but its type (what is instanceof Doubl...

Class is a raw type. References to generic type Class<T> should be parameterized

There are lots of threads about this type of question, but I want to get a complete answer and actually understand it without 'hiding' the problem with a @SupressWarnings ! I have the following class (from a simple Spring tutorial) public class CarValidator implements Validator { public boolean supports(Class aClass) { return Car.cla...

Why does Java not support type inference for constructors?

E.G. to create an ArrayList of Strings we have to do something like List<String> list = new ArrayList<String>(); whereas it should be able to infer the parameter type for the constructor so that we only have to type List<String> list = new ArrayList(); Why can't the type be infered in the same way that type parameters are infered f...

ASP.NET BasePage back referencing to concrete implementation

I have a page that is setup like this public partial class _Default : ViewBasePage<EmployeePresenter, IEmployeeView>, IEmployeeView { ... } Inside my base page public abstract class ViewBasePage<TPresenter, TView> : Page where TPresenter : Presenter<TView> where TView : IView { protected...

java iterator/iterable subinterface

I have an interface for a variety of classes, all of which should implement Iterator, so I have something like public interface A extends Iterable<A> { ...otherMethods()... } For the concrete classes, however, this means I must use public class B implements A { public Iterator<A> iterator() {...} } when I'd prefer (or at least, thi...

can't pass default comparer as IComparer<object>

I'm trying to call a "Sort" method that expects a parameter of type IComparer<object>, using the code: collection.Sort((IComparer<object>)Comparer<DateTime>.Default) It builds but at run time I get an InvalidCastException with the message: Unable to cast object of type 'System.Collections.Generic.GenericComparer`1[System.DateTime]' t...

Java Generics: casting to ? (or a way to use an arbitrary Foo<?>)

So, I have some code that looks approximately like (truncated for brevity - ignore things like the public member variables): public class GenericThingy<T> { private T mValue; public final T[] mCandidates; public GenericThingy(T[] pCandidates, T pInitValue) { mCandidates = pCandidates; mValue = pInitValue; ...

How to implement generic type-safe deep cloning in a Java class hierarchy?

I have a base class, say Base which specifies the abstract method deepCopy, and a myriad of subclasses, say A, B, C, ... Z. How can I define deepCopy so that its signature is public X deepCopy() for each class X? Right, now, I have: abstract class Base { public abstract Base deepCopy(); } Unfortunately, that means that if if I have...

WCF: Interfaces, Generics and ServiceKnownType

Hi guys I have the following: [ServiceContract] [ServiceKnownType(typeof(ActionParameters))] [ServiceKnownType(typeof(SportProgram))] [ServiceKnownType(typeof(ActionResult<SportProgram>))] public interface ISportProgramBl { [OperationContract] IActionResult<ISportProgram> Get(IActionParameters parameters); } When I run the ...

Calling IEnumerator from an Interface class

I am new to enumerating collections so forgive if this question sounds silly. I have an class public class PersonStuff : IPersonStuff, IEnumerable<User> { Person person = new Person(); ... IEnumerator<Person> IEnumerable<Person>.GetEnumerator() { return (person as IEnumerable<Person>).Ge...

Optional parametrization in Java with Generics

Is is it possible to specify default type when parametrzing a class? Example: // abstract class public abstract class AbsClass<T1 extends Par1Class, T2 extends Par2Class> { // code } // parametrized imlementation class public class RealClass extends AbsClass<ClassThatExtendsPar1, ClassThatExtendsPar2Class> { // cod...

Find type parameter of method return type in Java 6 annotation processor

I'm writing a tool that uses the annotation processor to generate source code depending on the return type of methods of an annotated class. The return type is always some subtype (interface or class) of an interface A that defines a type variable T. interface A<T>{T m();}; I would like to find the type parameter for the method m() r...

Can someone help with this generic extension method?

I'm trying to convert an XElement to either null or whatever type T is supplied. Here's what I have so far: public static T? ConvertToNullable<T>(this XElement element) where T : IConvertible { if (element.IsNill()) return null; else return (T)element; } Error: The type T must be a non-nullable value type ...

How do you use the Find method with a generic List class to find a specific instance by name

Hello, I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in Find method to find a particular ListBox by the Name of the Listbox I found the following MSDN article http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx This does not work for me because it does not show how to find a...

Java: Generic casting down generates warning, why?

I don't understand why the following code generates a warning. interface Generic<T> { } interface A { } class B { Generic<A> c; <T extends A> B(Generic<T> a) { c = (Generic<A>) a; //warning here } } //Unchecked cast from Generic<T> to Generic<A> In class B i'm only interested in using instances of Generic that ar...

How can i get generic type from a Class object ?

Hi, Suppose the following class public class Message { // some code } And a Spring Validator implementation (no Spring knowledge required) public class MessageValidator implements Validator { public boolean supports(Class clazz) { if(clazz.isAssignableFrom(Message.class)) return true; else if(<AN...

List<A> canot be sent to method( List<super-class-of-A> ) - why not?

I must have missed something regarding generics in Java, but why does this not work? List<String> list = new ArrayList<String>(); cannot be sent to: method( List<Object> ); But it does not work? How come? But if the method was: method( Object o ) it can be used with: method( new String("hello") ) without any problems q1) S...