Questions tagged [generic-method]

180 questions
3
votes
1 answer

How to convert List to Array t[] (for primitive types) using generic-method?

I am doing some tests with generic-methods and I'd like to transform these two methods below (convertFloatListToArray and convertShortListToArray) in just one (convertListToArray): public class Helper{ public static float[]…
Paulo
  • 2,956
  • 3
  • 20
  • 30
3
votes
1 answer

C# Reflection, using MakeGenericMethod with method that has the 'new()' type constraint

I am trying to use the MethodInfo MakeGenericMethod as follows: foreach (var type in types) { object output = null; var method = typeof (ContentTypeResolver).GetMethod("TryConstruct"); var…
David
  • 8,340
  • 7
  • 49
  • 71
3
votes
2 answers

Generic foreach loop for different property

I am trying to use a generic method for foreach loop which would pass different parameter as the argument. In this example below I would like to pass different parameters(EmployeeDisplayOrder or EmployeeEnrollOrder) public void…
Ell
  • 83
  • 1
  • 1
  • 7
3
votes
4 answers

Why Java Cannot Create Instances of Type Parameters

I know Java does not allow not Create Instances of Type Parameters. Many articles simply said "Type Erase" as the reason. But does type parameters initialization not occur before type erase? Is Type Erase the only reason? Here is a…
Tomi Liang
  • 31
  • 1
  • 4
3
votes
2 answers

Optimize Generic Method logic

I have one Generic Method, where we can pass a T as a interface type. Method returns a list of data corresponding T type. I have 20-25 same condition for this method how can I optimize logic. Class implements interface. example Student class…
user725388
3
votes
1 answer

How can I wrap a generic method around a generic method in scala?

I'm trying to wrap the spray-json parser such that it returns an Option rather than throws an exception. As a first step I'm just trying to wrap the method with my own, but I'm having problems making it generic. The parser uses an implicit format…
Russell
  • 12,261
  • 4
  • 52
  • 75
3
votes
1 answer

What's the correct usage of generic wildcards when defining functional Java APIs?

I'm writing functional-style static helper methods acting as operators for a generic abstraction (say Iterable), and I'm a bit confused about when I should use wildcards. What are the correct, most type-safe and simplest method signatures in the…
thSoft
  • 21,755
  • 5
  • 88
  • 103
3
votes
5 answers

How can I send Type variable to the method which is generic?

I have a method like this, public List Test() { // do something. } I don't know what is T and dont have. But I have type of T as TYPE. for example: class Person { } var type = typeof(Person); I don't have Person. Person is keeping at…
Sinan AKYAZICI
  • 3,942
  • 5
  • 35
  • 60
3
votes
3 answers

Returning a generic list

I'm trying to return a generic list after loading values from a file. However, after much fiddling with type manipulations I still can't get it to agree with me. The code is below; my questions are: Do I need to identify every key type like I'm…
Glinkot
  • 2,924
  • 10
  • 42
  • 67
2
votes
3 answers

Weird compilation error when indirectly refer to an assembly that declares a generic extension method with type restriction

Well, it's clear for me that the title of my question is too complicated. I just tried to make it as specific as possible. So, I'll try to explain the problem better. Problem context Let's assume we have three .NET projects in a solution. The main…
Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
2
votes
1 answer

How to wrap a class that has generic type methods with a class that has a generic type and no generic type arguments on the methods?

I have the following code example: class Stupid { private cache: Map = new Map(); get(key: string): T { return this.cache.get(key); }; } class Smart extends Stupid { get(key: string): T { super.get(key); } } I…
2
votes
5 answers

IList method() where T : Iclient can not add client object to list

public IList GetClientsByListofID(IList ids) where T : IClient { IList clients = new List(); clients.Add( new Client(3)); } I am getting a compiler error here: cannot convert from 'Bailey.Objects.Client' to 'T' The client…
Jon
  • 15,110
  • 28
  • 92
  • 132
2
votes
1 answer

Moq It.IsAnyType not working for Func returning Task with generic type

I've tried 3 different ways to setup a mock for a generic interface method. Only one way works, but it is using an explicit type, so won't work generically. I tried using It.IsAnyType, but it doesn't seem to match on the call that is made. Here is…
Anssssss
  • 3,087
  • 31
  • 40
2
votes
1 answer

Using 'diamond' notation for methods in java

I'm currently working on a component-based architecture management system in java. My current implementation of the retrieval of a component attached to an object works like this: // ... private final HashMap,…
duck
  • 23
  • 2
2
votes
2 answers

MOQ- Setting up and verifying a generic method with Func argument

I have a third party interface which I want to mock its methods. To make my purpose clear consider the following IFoo interface which has a generic method like M2. One of M2 arguments is of type Func. public interface IFoo { …
Sayari
  • 103
  • 5