generic-collections

Custom Collection (Of T)

依然范特西╮ 提交于 2019-12-13 02:57:24
问题 I'm trying to create a custom version of Collection that uses a type instead of Object . I have a working class (see below), however, I would like to change it to be (Of T) instead of having to create a new class for each type that I would like to use. Is it possible to implement this? Imports System.Collections.Generic Public Class CustomCollection Implements IDictionary(Of String, CustomClass) Private _dct As New Dictionary(Of String, CustomClass) #Region " IDictionary<string,object>

Generic Type parameters for covariant params array

时光怂恿深爱的人放手 提交于 2019-12-12 03:15:35
问题 I have an interface: IReadOnlyList<TEntity> Execute<TEntity>(ILambdaQuery<TEntity> query, params ILambdaQuery<TAssociatedEntity>[] associations) where TAssociatedEntity : class, IAggregateRoot; The interface is used to execute a query in a repository. Where TEntity is the type that you're expecting back and what you are querying and TAssociatedEntity are other queries that you'd like to run and that will be combined with the TEntity results that come back. Eg. var courseQuery = LambdaQuery

Java: Understanding Type Erasure with Generic Arrays

天大地大妈咪最大 提交于 2019-12-12 00:50:23
问题 I was wondering whether I understand the following Java issue correctly. Given a generic collection, if I do public class HashTable<V extends Comparable<V>> implements HashTableInterface<V> { private V[] array; public HashTable() { this.array = (V[]) new Object[10]; } } the code breaks, throwing an exception: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable; However, if I change this.array = (V[]) new Object[10]; to this.array = (V[]) new Comparable

Storing objects conforming to a protocol with generics in a typed array

余生颓废 提交于 2019-12-10 23:55:35
问题 I've got a protocol: protocol Adjustable: Equatable { associatedtype T var id: String { get set } var value: T { get set } init(id: String, value: T) } And a struct that conforms to it: struct Adjustment: Adjustable { static func == (lhs: Adjustment, rhs: Adjustment) -> Bool { return lhs.id == rhs.id } typealias T = CGFloat var id: String var value: T } And I'm building a wrapper class that behaves like a Set to handle an ordered list of these properties: struct AdjustmentSet { var

JSON generic collection deserialization

泪湿孤枕 提交于 2019-12-10 14:54:27
问题 I've such DTO classes written in Java: public class AnswersDto { private String uuid; private Set<AnswerDto> answers; } public class AnswerDto<T> { private String uuid; private AnswerType type; private T value; } class LocationAnswerDto extends AnswerDto<Location> { } class JobTitleAnswerDto extends AnswerDto<JobTitle> { } public enum AnswerType { LOCATION, JOB_TITLE, } class Location { String text; String placeId; } class JobTitle { String id; String name; } In my project there is Jackson

Linq and deferred evaluation

女生的网名这么多〃 提交于 2019-12-10 14:39:39
问题 When you use LINQ to define an enumerable collection, either by using the LINQ extension methods or by using query operators,the application does not actually build the collection at the time that the LINQ extension method is executed; the collection is enumerated only when you iterate over it. This means that the data in the original collection can change between executing a LINQ query and retrieving the data that the query identifies; you will always fetch the most up-to-date data.

Generic collections type test

强颜欢笑 提交于 2019-12-10 14:27:23
问题 I'd like to make some operations according to a given collection type (using reflexion), regardless of the generic type. Here is my code: void MyFct(Type a_type) { // Check if it's type of List<> if (a_type.Name == "List`1") { // Do stuff } // Check if it's type of Dictionary<,> else if (a_type.Name == "Dictionary`2") { // Do stuff } } It works for now, but it gets obvious to me that it's not the most safe solution. void MyFct(Type a_type) { // Check if it's type of List<> if (a_type ==

Can't understand the Exception when using dynamic with generic collection in .net4

試著忘記壹切 提交于 2019-12-10 13:45:38
问题 check the code below please: static void Main(string[] args) { IList<dynamic> items = new List<dynamic>(); items.Add(3); items.Add("solid"); dynamic i = new ExpandoObject(); items.Add(i); //System.Collections.Generic.IList<object>' does not contain a definition for 'Add' Console.WriteLine(); } is this a bug in "dynamic" mechanism? 回答1: Looks like a bug (or is it a feature request?): https://connect.microsoft.com/VisualStudio/feedback/details/534288/ilist-dynamic-cannot-call-a-method-add

How to filter a collection inside a generic method

杀马特。学长 韩版系。学妹 提交于 2019-12-10 11:48:56
问题 I have a two class which is having following properties Class A { public int CustID { get; set; } public bool isProcessed { get; set; } } Class B { public int EmpId{ get; set; } public bool isProcessed { get; set; } } I created one generic method which accepts all these classes.'isProcessed' property is common in both these classes. public void ProceesData<T>(IList<T> param1, string date1) { } I need following things Inside ProcessData method i want to filter items which is having isProcessed

Convert Dictionary into structured format string

南楼画角 提交于 2019-12-10 03:00:43
问题 I have a Dictionary object declared as var as Dictionary(of String, String) . I am trying to utilize the LINQ extensions available to the Generic Collection but am only getting the non-extension methods. I need to turn the Dictionary collection into a string with the following pattern: key1=val1, key2=val2, ..., keyn=valn Thought at first doing a foreach loop would hit the spot except the fact that i am programmers-block. What i have so far, but doubt its the best logic pattern for producing