typeparameter

Get Type Parameter T from an instantiated System.Type?

Good afternoon, I am somewhat lost and maybe can give me a quick push in the right direction: I have the System.Type of a certain object but need to pass that over as a Type Parameter T to another method... is that somehow possible? Or am I lost in the bigger picture there? Cheers and thanks, -J ...

C# get the types defining a Dictionary at run time

Hi there, I was wondering what is the best way for getting the generic arguments that definine a dictionary at run time is. Take for example: Dictionary<string, object> dict; How at runtime can I find out that the keys are strings? Thanks ...

What's wrong with this reflection code? GetFields() is returning an empty array.

C#, Net 2.0 Here's the code (I took out all my domain-specific stuff, and it still returns an empty array): using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ChildClass cc = n...

Can Scala allow free Type Parameters in arguments (are Scala Type Parameters first class citizens?)?

I have some Scala code that does something nifty with two different versions of a type-parameterized function. I have simplified this down a lot from my application but in the end my code full of calls of the form w(f[Int],f[Double]) where w() is my magic method. I would love to have a more magic method like z(f) = w(f[Int],f[Double])-...

How to instantiate an instance of type represented by type parameter in Scala

example: import scala.actors._ import Actor._ class BalanceActor[T <: Actor] extends Actor { val workers: Int = 10 private lazy val actors = new Array[T](workers) override def start() = { for (i <- 0 to (workers - 1)) { // error below: classtype required but T found actors(i) = new T acto...

How do I create a class that inherits from another and passes a type parameter in CodeDom?

Here's what I want the resulting class declaration to look like: public sealed partial class Refund : DataObjectBase<Refund> { } } This code (snipped): targetClass = new CodeTypeDeclaration(className); targetClass.IsClass = true; targetClass.TypeAttributes = TypeAttributes.Public | TypeAttrib...

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...

EnvDTE.CodeClass.Base.Fullname does not give proper name for VB types

Public Class c1(Of T) End Class Public Class c2 Inherits c1(Of Integer) End Class For the following peice of code, if we try to get the CodeClass.Base.Fullname, it gives the fullname as "c1(Of T)". Ideally it should give the fullname of the instance i.e. "c1(Of Integer)". The CodeModel API behaves differently for VB and C# codes. ...

How do I extend Java interface containing generic methods in Scala?

Suppose we have the following Java interface: // Java public interface Foo { <T> T bar(Class<T> c); } How should I extend it in Scala? Writing // Scala class FooString extends Foo { override def bar(c: Class[String]): String = "hello, world"; } will cause the compiler to throw "class FooString needs to be abstract, since meth...

Instantiating a Type Parameter Without Passing an Object

My question is very similar to this question. I want to be able to instantiate an object of the type parameter type, but also without needing to pass in a "factory". I really need to be contained all in the one class. public class myClass<E> { E someObject; public myClass(){ someObject = new E(); } } Previous soluti...

C# - Type Parameters in Constructor - No Generics

I have a class that I am trying to do unit tests on. The class is a WCF Service Class. (Making it a generics class is not my goal.) I have a data access layer (DAL) type (called UserDAL) that is instantiated in many methods. To get these methods under test, I need to get this local variables mocked. (Each instance of UserDAL has met...

Circular type parameters definition in scala

I am trying to define a generic container whose elements can return the enclosing container. Something like: abstract class Container[E <: Element] { // compile error def contains( e: E ): Boolean def addNewElement(): Unit } abstract class Element[C <: Container] { // compile error def enclosingContainer(): C } class MyContainer...

C# type parameters specification

Some special CLI types from mscorlib library (ArgIterator, TypedReference and RuntimeArgumentHandle types) cannot be used as generic type parameters to construct the generic types / methods: void Foo<T>() { } void Bar() { Foo<ArgIterator>(); } provides the compiler error: error CS0306: The type 'System.ArgIterator' may not be used as...

How do I access and use generic type parameters as a regular type in C#?

I have a generic business object collection class which contains some business objects: public abstract class BusinessObjectCollection<T> : ICollection<T> where T : BusinessObject I want to write a method on my Collection class that returns the type T and a method that returns a newly-instantiated object of type T. In C++, thi...

parameter extends a class

Hello, I want to do a class thats accepts anything ordered and prints greater. (I'm just learning so I know it's a bit useless) class PrinterOfGreater[T extends Ordered](val a:T, val b:T){println(a > b)} I know that it can't be written by this style in scala, but I don't know how to write it properly... Do anybody know? and why this ...

Default type-parametrized function literal class parameter

Is this an intended behavior or is it a bug? Consider the following trait (be it a class, doesn't matter): trait P[T] { class Inner(val f: T => Unit = _ => println("nope")) } This is what I would have expected: scala> val p = new P[Int] { | val inner = new Inner | } p: java.lang.Object with P[Int]{def inner: this.In...

extension methods with generics - when does caller need to include type parameters?

Hi, Is there a rule for knowing when one has to pass the generic type parameters in the client code when calling an extension method? So for example in the Program class why can I (a) not pass type parameters for top.AddNode(node), but where as later for the (b) top.AddRelationship line I have to pass them? class Program { stati...

Call a method of type parameter

Is there any way to do code such this: class GenericClass<T> { void functionA() { T.A(); } } Or, how to call a function of type parameter (type is some my custom class). ...

F#: explicit type parameters in operator binding

I'm trying to define operator with the explicit type parameters and constraints: let inline (===)<'a, 'b when 'a : not struct and 'b : not struct> a b = obj.ReferenceEquals (a,b) It works well in F# 2.0, but produces the: warning FS1189: Type parameters must be placed directly adjacent to the type name, e.g. "type C<'...

Problem with Java Generics, type parameter, and returned list

Hello! This code is simplified as much as I can from a more complex class structure. In the real code, there were sub-types of the Integer and Double types I use here. I'm trying to use Java Generics with a type parameter. If the user requests the type of Number.class, we want to combine the List<Integer> list and the List<Double> list...