generics

Conditional types with Generics in TypeScript

我的未来我决定 提交于 2020-05-29 07:25:15
问题 What I want to achieve can be best explained in code: Given shoe and dress class: class Shoe { constructor(public size: number){} } class Dress { constructor(public style: string){} } Have a generic box that can only contain Shoe or Dress. Can't contain both: class Box <T extends Shoe | Dress > { } Then have a utility class that takes care of moving shoes: class ShoeMover { constructor(public size: number[]){} } Also, a utility class for moving Dresses: class DressPacker { constructor(public

C# type arguments cannot be inferred from usage in Select with multiple returns

試著忘記壹切 提交于 2020-05-28 12:45:40
问题 I don't think I'm doing anything too esoteric, but I don't see any other questions about this. The following code (I've reduced it to the essentials) generates a compiler error in C# 4. However, it should be obvious what the type argument is - there's a greatest common denominator ("class A") that is also explicitly defined in the return type of the method "Frob". Shouldn't the compiler make a list of all the return types in the lambda expression, create an ancestry tree to find their common

C# type arguments cannot be inferred from usage in Select with multiple returns

末鹿安然 提交于 2020-05-28 12:44:19
问题 I don't think I'm doing anything too esoteric, but I don't see any other questions about this. The following code (I've reduced it to the essentials) generates a compiler error in C# 4. However, it should be obvious what the type argument is - there's a greatest common denominator ("class A") that is also explicitly defined in the return type of the method "Frob". Shouldn't the compiler make a list of all the return types in the lambda expression, create an ancestry tree to find their common

C# type arguments cannot be inferred from usage in Select with multiple returns

吃可爱长大的小学妹 提交于 2020-05-28 12:44:10
问题 I don't think I'm doing anything too esoteric, but I don't see any other questions about this. The following code (I've reduced it to the essentials) generates a compiler error in C# 4. However, it should be obvious what the type argument is - there's a greatest common denominator ("class A") that is also explicitly defined in the return type of the method "Frob". Shouldn't the compiler make a list of all the return types in the lambda expression, create an ancestry tree to find their common

The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable<T>'

一笑奈何 提交于 2020-05-28 12:07:25
问题 I'm using .net framework 4.5 I get the following error Error CS0453 The type 'MyObject' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'Nullable' public async Task<Nullable<MyObject>> MyMethod(string myParamter) {} I also tried public async Task<MyObject?> MyMethod(string myParamter) {} If I set the thing as nullable then why do I get the red line below the method name with this error message The stackoverflow answer is easy, make the

AutoMapper generic conversion

会有一股神秘感。 提交于 2020-05-27 11:49:05
问题 I've been using AutoMapper and would like to take generic conversion one step further; instead of saying something like cfg.CreateMap<Container<int>, int>() .ConvertUsing(new ContainerConverter<Container<int>, int>()); I would rather set the AutoMapper to know how to map any Container, such as: cfg.CreateMap<Container<T>, T>() .ConvertUsing(new ContainerConverter<Container<T>, T>()); Since all conversions from Container to T are the same, it would be pointless to re-define this conversion for

Force derived class to implement interface

本秂侑毒 提交于 2020-05-27 03:26:10
问题 I am here today (like yesterday) with another weird interface question. I have a class: public class InputDevice<T> where T : Button { protected List<T> buttons = new List<T>(); protected InputDevice() { //Only allow instanciation from within a derived class } } As you can see, this class cannot be instantiated. A class that derives from it might be able to be instantiated. This is a subclass: public sealed class Keyboard : InputDevice<KeyboardButton> { public Keyboard() { buttons.Add(new

Spring Data (JPA) - using generics in @Query

隐身守侯 提交于 2020-05-26 10:20:51
问题 I wonder if it's possible to use generics in the named queries in spring data (using jpa myself), is it possible to do anything like this? @NoRepositoryBean public interface EnumerationRepository<T extends Enumeration> extends JpaRepository<T,Integer> { @Query("Select t.type from T t") public List<String> selectTypes(); } Enumeration class being this @MappedSuperclass public abstract class Enumeration { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", length = 3)

Create List<int> with values at compile time

独自空忆成欢 提交于 2020-05-25 08:59:29
问题 It is possible to create an array at compile time like; int[] myValues = new int[] { 1, 2, 3 } ; But I would like to do something like this; List<int> myValues = new List<int>() { 1, 2, 3 }; The compiler says No. Is there a way to do this (C# 2.0) without using LINQ (C# 3.0)? 回答1: List<int> myValues = new List<int>(new int[] { 1, 2, 3 } ); This will create an intermediate array however so there may be a more efficient way of doing the same thing. EDIT: John Feminella suggested creating a

Sort a List so a specific value ends up on top

穿精又带淫゛_ 提交于 2020-05-25 06:21:49
问题 I have a class Offer which contains a filed Category. I want all Offers of a specific category to appear on top, followed by all else. I tried this, but to no avail, what would you recommend? Offers = Offers.OrderBy(x => x.Category == "Corporate").ToList(); 回答1: When you order by a boolean value false (0) comes before true (1). To get the elements that match the predicate first you should reverse the sort order by using OrderByDescending : Offers = Offers.OrderByDescending(x => x.Category ==