generics

Access class property passed as generic type

情到浓时终转凉″ 提交于 2021-02-05 08:59:05
问题 I have two classes, which are passed to Serialization method and I would like to access two properties of these classes in Serialization method. The problem is that Serialization method parameter are passed as generic type and I do not know how to access properties of passed class in this case. The example below. public class MyClass1 { public string MyProperty1 { get; set; } //These properties are shared in both classes public bool Result { get; set; } public string EngineErrorMessage { get;

Cannot Resolve Method when using generics

大兔子大兔子 提交于 2021-02-05 08:13:10
问题 I am trying to implment a tree for my project. This tree will contain nodes which are different board states after some move. Right now my project is structured like so: src Agent Support Test Threes Tree.java Some class.java some class Board.java I want my tree to store these Board objects kept in package src.Threes. However in my Tree class it is not letting me access my public board methods. I am confident that I am just missing some really basic thing but I cannot figure this out after

Extract FieldType key and value from HList

ぐ巨炮叔叔 提交于 2021-02-05 08:12:56
问题 I would like to extract the key and value of the head of an HList using these two methods: def getFieldName[K, V](value: FieldType[K, V])(implicit witness: Witness.Aux[K]): K = witness.value def getFieldValue[K, V](value: FieldType[K, V]): V = value I tried a few variations of this function, but I couldn't make it work, I think this might be the closest to the right solution: def getFieldNameValue[Key <: Symbol, Value <: AnyRef, Y <: HList, A <: Product](a : A) (implicit gen : LabelledGeneric

TypeScript `groupBy` typings

こ雲淡風輕ζ 提交于 2021-02-05 07:10:50
问题 I want to write a groupBy function, so far the type system is not so happy with my work. export function group< T extends object, K extends (keyof T & string), R = T[K] extends string ? string : never >( data: T[], groupBy: keyof T ): { [group: R]: T[] } The first error that I got is that R in { [group: R]: T[] } is not string or number . The signature actually doesn't work at all for the data set of group([{ name: 'Johnny Appleseed', age: 17 }], 'name') // R should be string group([{ name:

Why doesn't this code work when wrapped in a generic function?

强颜欢笑 提交于 2021-02-05 06:59:26
问题 I use this code to perform a HTTP POST request and deserialize the returned value: ParameterizedTypeReference<MyClass> typeRef = new ParameterizedTypeReference<>() {}; HttpEntity<Object> requestEntity = new HttpEntity<>("some text"); ResponseEntity<MyClass> result = restTemplate.exchange("/test", HttpMethod.POST, requestEntity, typeRef); MyClass returnValue = result.getBody(); To make it easier to use, I tried to wrap the code in a function like so: public <T> T post(Object content, Class<T>

java generics: can't create a simple print array method

房东的猫 提交于 2021-02-05 06:35:07
问题 So i keep on getting an error on the next code. And I have no idea what i'm doing wrong. The error is The method printArray(T[]) in the type main is not applicable for the arguments (int[]) public class main { public static void main(String[] args) { Oefening1 oef = new Oefening1(); int[] integerArray = {1,2,3,4,5}; printArray(integerArray); } public static <T> void printArray(T[] arr){ for(T t: arr){ System.out.print(t + " "); } System.out.println(""); } } 回答1: When it comes to generics,

C # equivalent of Java List<? extends Class>

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-05 06:28:45
问题 I have a basic structure of generic's classes public class Parent<T> where T : Parent<T> { Action<T> Notify; } public class Child : Parent<Child> { } And I want to have a list so that Child objects can be put there List<Parent> parents = new List<Parent>(); In java I just can write List<? extends Parent> and all Parent's subclasses easily can be added to the list. Is there any alternative of this in C#? 回答1: You can't do the same thing as Java, because in Java, generics use type erasure and

Overloading a method which takes in Generic type causes ambiguous method call compile error

家住魔仙堡 提交于 2021-02-05 06:21:08
问题 See below simple snippet: public class GenericsOverloadingDistinguish<T> { public void print1(T t) { System.out.println("t"); } public void print1(Integer i) { System.out.println("integer"); } } public static void main(String[] args) { new GenericsOverloadingDistinguish<Integer>().print1(new Integer(1)); } This would cause an ambiguous method call and will not compile. This is utterly confusing on the user of that class. It is not able to call neither print1(T t) nor print1(Integer i) simple

A keyword to reference the current object as a generic type

自作多情 提交于 2021-02-05 06:09:54
问题 I have a simple generic delegate: delegate void CommandFinishedCallback<TCommand>(TCommand command) where TCommand : CommandBase; I use it in the following abstract class: public abstract class CommandBase { public CommandBase() { } public void ExecuteAsync<TCommand>(CommandFinishedCallback<TCommand> callback) where TCommand : CommandBase { // Async stuff happens here callback.Invoke(this as TCommand); } } While this does work, I have no way of forcing the TCommand passed into Execute to be

Infer generic type with two generic type parameters [duplicate]

假如想象 提交于 2021-02-05 05:23:32
问题 This question already has answers here : Partial generic type inference possible in C#? (5 answers) Closed 2 years ago . I have the following method public bool HasTypeAttribute<TAttribute, TType>(TType obj) { return typeof(TType).GetCustomAttribute<TAttribute>() != null; } and I want to be able to use it like this: MyClass instance = new MyClass(); TypeHelper.HasTypeAttribute<SerializableAttribute>(instance); but I can't get it working because of the incorrect number of type parameters so