generics

C# Generic Method, cannot implicit convert

纵饮孤独 提交于 2020-04-06 04:52:12
问题 I've got the following code: public static T GetCar<T>() where T : ICar { T objCar = default(T); if (typeof(T) == typeof(SmallCar)) { objCar = new SmallCar(""); } else if (typeof(T) == typeof(MediumCar)) { objCar = new MediumCar(""); } else if (typeof(T) == typeof(BigCar)) { objCar = new BigCar(""); } return objCar; } And this is the error I am getting: Cannot implicitly convert type 'Test.Cars' to 'T' What Am I missing here? All car types implement the ICar interface. Thanks 回答1: You cannot

How can I call generic method unknowing instance object type

家住魔仙堡 提交于 2020-04-05 05:24:38
问题 With this code: World w = new World(); var data = GetData<World>(w); If I get w with reflection and this can be of type World , Ambient , Domention , etc. How can I get GetData ??? I only have the instance object: var data = GetData<???>(w); 回答1: var type = <The type where GetData method is defined>; var genericType = typeof(w); var methodInfo = type.GetMethod("GetData"); var genericMethodInfo = methodInfo.MakeGenericMethod(genericType); //instance or null : if the class where GetData is

How can I call generic method unknowing instance object type

孤街浪徒 提交于 2020-04-05 05:24:07
问题 With this code: World w = new World(); var data = GetData<World>(w); If I get w with reflection and this can be of type World , Ambient , Domention , etc. How can I get GetData ??? I only have the instance object: var data = GetData<???>(w); 回答1: var type = <The type where GetData method is defined>; var genericType = typeof(w); var methodInfo = type.GetMethod("GetData"); var genericMethodInfo = methodInfo.MakeGenericMethod(genericType); //instance or null : if the class where GetData is

How to use an integer array for a generic method?

♀尐吖头ヾ 提交于 2020-03-26 06:08:06
问题 My insertion sort method is declared using this statement: public static <AnyType extends Comparable<? super AnyType>> void insertionSort(AnyType[] a){ I am having trouble understanding how to make this generic array type parameter accept an array of primitive type int . How would I call this method with int[] as my parameter, or at least how would it be used to sort an int array? 回答1: Use a wrapper class - Integer. There is something called autoboxing which means that if you pass an argument

How to get annotations in nested parameter parameter?

浪子不回头ぞ 提交于 2020-03-26 04:02:13
问题 If I have a function like this, public void park( List< @myNote("list") List< @myNote("integer") Integer>> l){ System.out.println("park"); } If I use Parameter.getAnnotations() and Paramter.getDeclaredAnnotation, I can only get @myNote("list"). Are any ways to get the annotation @myNote("integer")? This may be related to How to get all types of the inner structures of a nested type? 回答1: Here is code to print the annotations, so you can see where in the reflection API they can be found.

What is the proper way to handle a generic object in a WEBAPI method?

。_饼干妹妹 提交于 2020-03-20 06:56:06
问题 The scenario is something like this: I have this web-api which will handle a variety of payment gateways, and I want to have the same endpoint for all of those. So, what I have in mind is to get some json data like this: { "OperationId":"N0004", "Generic_Object_That_Will_Change_According_ToThe_GateWay": { "Sale_id":1000, "CodUser":"1000040", "Email":"teste@teste.com" } } Or this, for some other payment gateway { "OperationId":"N044444", "Generic_Object_That_Will_Change_According_ToThe_GateWay

Generic way to check if range contains value in Scala

泪湿孤枕 提交于 2020-03-19 06:45:52
问题 I'd like to write a generic class that holds the endpoints of a range, but the generic version kicks back a compilation error: value >= is not a member of type parameter A final case class MinMax[A <: Comparable[A]](min: A, max: A) { def contains[B <: Comparable[A]](v: B): Boolean = { (min <= v) && (max >= v) } } The specific version works as expected: final case class MinMax(min: Int, max: Int) { def contains(v: Int): Boolean = { (min <= v) && (max >= v) } } MinMax(1, 3).contains(2) // true

Derive from specialized generic types

…衆ロ難τιáo~ 提交于 2020-03-19 06:01:05
问题 Is it possible to derive a class from a specialized generic type: TGenericBase <T> = class // ... end; TSpecializedDerived = class (TGenericBase <String>) // ... end; Just wondering if this is possible at all... EDIT Code works fine when I put it in a new project. Must be due to some other mistake; sorry about that 回答1: Yes. I do it all the time. It's very useful. One of my favorite tricks goes something like this: TSpecializedList = class(TObjectList<TMyType>) public (extra methods specific

Generic Method Parameters in Golang

让人想犯罪 __ 提交于 2020-03-18 17:12:10
问题 I need help with making this work for any type. I have got a function I need to accept other types that have ID property. I have tried using interfaces but that did not work for my ID property case. Here is the code: package main import ( "fmt" "strconv" ) type Mammal struct{ ID int Name string } type Human struct { ID int Name string HairColor string } func Count(ms []Mammal) *[]string { // How can i get this function to accept any type not just []Mammal IDs := make([]string, len(ms)) for i,

Generic Method Parameters in Golang

岁酱吖の 提交于 2020-03-18 17:10:17
问题 I need help with making this work for any type. I have got a function I need to accept other types that have ID property. I have tried using interfaces but that did not work for my ID property case. Here is the code: package main import ( "fmt" "strconv" ) type Mammal struct{ ID int Name string } type Human struct { ID int Name string HairColor string } func Count(ms []Mammal) *[]string { // How can i get this function to accept any type not just []Mammal IDs := make([]string, len(ms)) for i,