generics

Java: how do I get a class literal from a generic type?

Typically, I've seen people use the class literal like this: Class<Foo> cls = Foo.class; But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized: Class<List> cls = List.class So why not add a <?>? Well, this causes a type mismatch error: Class<List<?>> cls = List.class I ...

How to dynamcially call a generic method based on a mapping in a dictionary?

I have a method String Foo<T> where T: WebControl Now I do have a string like "hyperlink". What is want is to call Foo<Hyperlink> based on a mapping from the string to the generic. How does the dictionary have to look like? It ain't: private Dictionary<string, Type> _mapping = new Dictionary<string, Type>() { {"hyperlink", ty...

C# generic constraints

Is it possible to enumerate which types that is "available" in a generic constraint? T MyMethod<t>() where T : int, double, string Why I want to do this is that I have a small evaluator engine and would like to write code like this: bool expression.Evaluate<bool>(); or int expression.Evaluate<int>(); but i want to prohibit MyC...

Problem in calling function parameter as generics dictionary from C# to vb.net

I have a function written in C# which has one parameter type as Dictionary<string , string>. When i try to call this function in VB.Net it doesn't show the type as Dictionary<string , string> it shows as string. Below is my function public bool RegisterCustomerAttribute(int CustomerId , Diction...

C++ templates hides parent members

Usually, when A is inheriting from B, all the members of A are automatically visible to B's functions, for example class A { protected: int a; }; class B : public A { int getA() {return a;} //no need to use A::a, it is automatically visible }; However when I'm inheriting with templates, this code becomes illegal (at least...

List assignment from child to parent

I am trying to do this: List<Parent> test = new List<Child>(); The complete code of my class is this: class Program { public static void Main(string[] args) { List<Parent> test = new List<Child>(); test.Add(new Child()); test.Add(new AnotherChild()); } } class Parent { } class Child : Parent { ...

Need help understanding Generics, How To Abstract Types Question.

I could use some really good links that explain Generics and how to use them. But I also have a very specific question, relater to working on a current project. Given this class constructor: public class SecuredDomainViewModel<TDomainContext, TEntity> : DomainViewModel<TDomainContext, TEntity> where TDomainContext : Doma...

How can I sort just part of a huge list using .NET?

In .NET, the Generics Lists have a sort function that accepts IComparer or Comparison. I'd like to sort just part of a list. Hopefully I can specify the start index, count of elements to sort, and a lambda function. It looks like you can only use lambda functions to do this if you're sorting the entire list. Is that right or did I mi...

How to get type of TKey and TValue given a Dictionary<TKey,TValue> type

I want to get type of TKey and TValue given a Dictionary<TKey,TValue> type. eg. If type is Dictionary<Int32,String> I want to know how to get keyType = typeof(Int32) and valueType = typeof(String) ...

How can I use Convert.ChangeType to convert string into numerics with group separator?

Hello, I want to make a generic string to numeric converter, and provide it as a string extension, so I wrote the following code: public static bool TryParse<T>( this string text, out T result, IFormatProvider formatProvider ) where T : struct try { result = (T)Convert.ChangeType( text, typeof( T ), formatProvider ); return true;...

Generic Lists copying references rather than creating a copiedList

I was developing a small function when trying to run an enumerator across a list and then carry out some action. (Below is an idea of what I was trying to do. When trying to remove I got a "Collection cannot be modified" which after I had actually woken up I realised that tempList must have just been assigned myLists reference rather t...

Calling a method with an arg of Class<T> where T is a parameterized type

I'm attempting to call a constructor method that looks like: public static SomeWrapper<T> method(Class<T> arg); When T is an unparameterized type like String or Integer, calling is straightforward: SomeWrapper<String> wrapper = method(String.class); Things get tricky when T is a parameterized type like List<String>. The following ...

Instantiating a list of parameterized types, making beter use of Generics and Linq

I'm hashing a file with one or more hash algorithms. When I tried to parametrize which hash types I want, it got a lot messier than I was hoping. I think I'm missing a chance to make better use of generics or LINQ. I also don't like that I have to use a Type[] as the parameter instead of limiting it to a more specific set of type (H...

.NET converting simple arrays to List Generics

This question might seem trivial and also stupid at the first glance, but it is much more than this. I have an array of any type T (T[]) and I want to convert it into a List generic (List<T>). Is there any other way apart from creating a Generic list, traversing the whole array and adding the element in the List? Present Situation: st...

LINQ-to-SQL: Searching against a CSV

I'm using LINQtoSQL and I want to return a list of matching records for a CSV contains a list of IDs to match. The following code is my starting point, having turned a CSV string in a string array, then into a generic list (which I thought LINQ would like) - but it doesn't: Error Error 22 Operator '==' cannot be applied to operands of ...

returning a Void object

What is the correct way to return a Void type, when it isn't a primitive? Eg. I currently use null as below. interface B<E>{ E method(); } class A implements B<Void>{ public Void method(){ // do something return null; } } ...

Generic Factorial function in C#

I want to write a generic function to calculate factorial in C# ... like: static T Factorial<T>(T n) { if (n <= 1) return 1; return Factorial<T>(n - 1); } but obviously having restriction that we can't perform operations on type 'T'. any alternative? ...

Subclasses of a class that implements a generic interface

I'm working with Google Web Toolkit, and I'm having problems implementing a generic interface. I'm not really familiar with generics, doing an upgrade on someone else's code here. Here's what I want to do: I want to have an implementation of a generic callback interface that does some logging, and then subclass that implementation in or...

[C#]Test if a class can be serialized

I want to create a generic method to serizlize a class to text (for use as part of a networking component) The method should be something like: public string SerializeToText<T>(T DataToSerialize); The method contents would simply perform the xml serialization and I can do this. What I want to know is if I can check whether T can be s...

Java interfaces and return types.

Consider I have the following interface: public interface A { public void b(); } However I want each of the classes that implement it to have a different return type for the method b(). Examples: public class C { public C b() {} } public class D { public D b() {} } How would I define my interface so that this was possibl...