Questions tagged [generic-method]
180 questions
0
votes
1 answer
Only permits children classes as generic parameters
Java 17
I'm implementing the following method:
public static Optional toOption(T t, Function super O, ? extends Optional> f, Class cls){
Optional opt;
if(cls.isAssignableFrom(t.getClass())){
opt =…

Some Name
- 8,555
- 5
- 27
- 77
0
votes
0 answers
System.Reflection.RuntimeMethodInfo cannot be converted to type System.Linq.Expressions.Expression
I have the following method:
Task> FindAsync(Expression> expression, IEnumerable columns = null, CancellationToken cancellationToken = default(CancellationToken)) where T : BaseOdataModel;
This is the way we…

Valmir Cinquini
- 371
- 7
- 17
0
votes
4 answers
Is there a way to write a generic method that fills lists of different types?
I have a parent class called Snack with subclasses Drink and Sweets. I want to store my Snacks in a "VendingMachine" Class where there is a list for each of the Products. However, I don't want to write the same method for each type of Snack. How…

luffydavor
- 3
- 2
0
votes
1 answer
Generic type get method doesn't work in a foreach loop - java: incompatible types: java.lang.Object cannot be converted to Error
I am studying Generics in Java in Oracle docs. In Type Inference & Generic Methods section there is this code and it is not working in Intellij Idea.
This is Box class
package org.example;
public class Box {
private T t;
public void…

SmokingTurtle
- 25
- 6
0
votes
1 answer
Accessing a public member from within a generic method
I'm trying to have some toolbox with each tool in that toolbox being something quite different in functionality. So every tool would require its own config to be created and passed at the start, or when you need to change params, while the base…

Trigger
- 3
- 1
0
votes
2 answers
Allow method to work with all inherited types
I have a class A from which B and C inherit.
I also have lists of B and C like so: List listB and List listC.
I want to add elements to these lists but only after I do some logic. I have made a method that takes a list of any type and a…

Lae
- 832
- 1
- 13
- 34
0
votes
1 answer
How I get the Model Class to when I am passing in a generic in my method?
I m using tracker-enabled-dbcontext and using documentation to capture DB audit logs, configure tracking we need to be used
EntityTracker
.TrackAllProperties()
.Except(x => x.Description)
.And(x => x.Id);
I have multiple Model classes…

user3732708
- 623
- 1
- 9
- 20
0
votes
1 answer
How to implement generic interface
I'm learning Generics in Typescript and have one problem with implementation generic interface.
My interface
export interface ToTable {
value: T
toColumns(): Column
fromColumn():T
}
I want to class which implements this interface…

irek
- 1
- 1
0
votes
1 answer
Creating methods by specifying LinkedHashMaps with generic values as a parameter
Let's say I have a super class Car and my sub classes are Honda, Toyota, and Lexus.
I have 3 LinkedHashMaps using each sub class as the value:
LinkedHashMap hondaModels = new LinkedHashMap();
LinkedHashMap

phamous
- 177
- 10
0
votes
1 answer
How to reference overridden generic method local parameter?
I have a main class that looks like this:
public abstract class Soldier : Monobehaviour
{
public virtual T GetClosestEnemy(T soldierType) where T : MonoBehaviour
{
T[] soldiers;
soldiers = FindObjectsOfType(soldierType);
T…

Deniz Demir
- 223
- 1
- 2
- 8
0
votes
0 answers
C# How to make more clear Generic Methods
I am trying to make a generic method about executing custom commands in MVC base system.
I have a method called DispatchCommand. I give that method to command type and command parameter type. My command type inherited from a MVCCommand
public…

Umutcan Ertürk
- 155
- 1
- 3
- 11
0
votes
1 answer
Setting generic variable value in Java
I have a global variable page
private T page
In addition, I have a setter method;
public void setGenericVar(T page) {
this.page = page
}
My calls in the main program are like this;
setGenericVar("one");
setGenericVar(1);
The error I'm getting…

paugarreina
- 11
- 3
0
votes
0 answers
How to dynamically call a generic method knowing only the string's name of a generic type parameter at runtime?
I want to insert an object into my repository but not explicitly like that at coding-time:
_repository().Add(carObject);
Instead of this, I want to push the object to the corresponding class name at runtime.
I can create the object at runtime…

Tareq Mahmud Hridoy
- 21
- 7
0
votes
2 answers
The type 'int' cannot be used as type parameter 'T' in the generic method. There is no boxing conversion from 'int'
I'm trying to implement Sorting trash container. Unfortunately, I'm not so experienced in C# yet and I'm obviously not getting the point of handling the generic method and corresponding interfaces. Below is some part of my code.
static void…
0
votes
1 answer
How to pass an anonymous object to a generic method with multiple generic types?
Let's say I have this generic method:
public T1 GenericTest(T2 Obj, out T2 NewObj)
{
NewObj = Obj;
return default;
}
How do I pass an anonymous object to it?
What do I put in place of the "???" when calling the method in the…

user3163495
- 2,425
- 2
- 26
- 43