generics

Java Generics & Reflection

北慕城南 提交于 2020-01-05 10:33:41
问题 this probably is a basic question, but can I do something like this: Class myClass = Class.forName("Integer"); SomethingSimple<myClass> obj; Where SomethingSimple is a very simple generic class: class SomethingSimple<T> { T value; SomethingSimple() {} public void setT(T val) { value = val; } public T getT() { return value; } } Obviously, the code above is not correct, since myClass is an object of type Class, and a class is required. The question is how can this be achieved. I read the other

Getting A Type-Specific Response From A WCF REST Generic Request?

杀马特。学长 韩版系。学妹 提交于 2020-01-05 08:47:07
问题 I am designing a WCF REST service. A requirement for the design is that the client is unaware of the particulars of a given request. For example, the following request: https://www.domain.com/dashboard/group/id/0 Would return: Request: GetGroup(GroupId = 0) Response: { Title="Country", children = { title="USA", Id=1, type=GROUP}, {title="England", Id=2, type=GROUP} } } And the following request: https://www.domain.com/dashboard/group/id/3 Would return: Request: GetGroup(groupId = 3) Response:

Generic List in C using void pointer

一个人想着一个人 提交于 2020-01-05 08:24:13
问题 I'm trying to create a generic list that will allow any type to be entered. However, I am having problems with the comparism in the is_element_of function (since I am making use of void pointers). Any help? typedef struct Item{ void* data; } Item; typedef struct Node{ Item Item; struct Node* next; struct Node* previous; } Node; typedef Node* List; bool is_element_of(Item Item, List *pointertolist) { bool isinlist = false; Node *scan = *pointertolist; while (scan->next != NULL) { if ((scan-

Calling static generic LINQ extension method in PowerShell

↘锁芯ラ 提交于 2020-01-05 08:17:17
问题 One can call many LINQ methods in PowerShell with this simple notation: [int[]] $numbers = 1..10000 [Linq.Enumerable]::Sum($numbers) It is even a relatively simple matter to include a lambda in a call: [Func[int,int]] $delegate = { $n = $args[0]; if ($n % 3) { $n } else { -$n } } [Linq.Enumerable]::Sum($numbers, $delegate) What I am wondering, though, is how to call a generic LINQ method from PowerShell: is it even possible? I found this SO question that seems to indicate one can, but I have

C# Generics: Explicit restate of inherited constraints in overriden methods could break polymorphism?

馋奶兔 提交于 2020-01-05 07:51:09
问题 Mark Michaelis wrote in his book ( C# 4.0 Essentials ): class EntityBase<T> where T : IComparable<T> { public virtual void Method<T>(T t) where T : IComparable<T> { // ... } } class Entity<T> : EntityBase<T> where T : IComparable<T> { public override void Method<T>(T t) // Error: Constraints may not be // repeated on overriding members where T : IComparable<T> { // ... } } However, overriding members need to conform to the “interface” defined in the base class method. Additional constraints

How to downcast an instance of a Generic type?

邮差的信 提交于 2020-01-05 07:50:46
问题 I'm struggling with how to properly use C# Generics. Specifically, I want to have a method that takes a Generic Type as a parameter and does different things based on the type of the Generic. But, I cannot "downcast" the Generic Type. See example below. Compiler complains about the cast (Bar<Foo>) saying "Cannot convert type Bar<T> to Bar<Foo> ". But at runtime the cast is OK since I've checked the type. public class Foo { } public class Bar<T> { } // wraps a Bar of generic type Foo public

Raw Type with List<String> gives compilation error [duplicate]

送分小仙女□ 提交于 2020-01-05 07:40:01
问题 This question already has answers here : Combining Raw Types and Generic Methods (5 answers) Why does this class behave differently when I don't supply a generic type? (2 answers) Closed 6 years ago . I have the following Generic class: import java.util.ArrayList; import java.util.List; public class GenericRaw<T> { public List<String> get() { return new ArrayList<>(); } } Let us consider the case of its usage: public class Usage { public void doSomething() { GenericRaw base = new GenericRaw()

Cast a raw map to a generic map using a method, cleanly and safely in a fail early manner

被刻印的时光 ゝ 提交于 2020-01-05 07:28:06
问题 Casting, instanceof, and @SuppressWarnings("unchecked") are noisy. It would be nice to stuff them down into a method where they won't need to be looked at. CheckedCast.castToMapOf() is an attempt to do that. castToMapOf() is making some assumptions: (1) The map can't be trusted to be homogeneous (2) Redesigning to avoid need for casting or instanceof is not viable (3) Ensuring type safety in an fail early manner is more important than the performance hit (4) Returning Map<String,String> is

Ninject and binding generics

微笑、不失礼 提交于 2020-01-05 06:55:08
问题 Warning : I'm just starting to explore Ninject . I have a generic DomainObject class defined as this: public abstract class DomainObject<T> : IDomainObject where T : IDomainObject { protected DomainObject(IDataProvider<T> dataProvider) { DataProvider = dataProvider; } // blah and blih protected IDataProvider<T> DataProvider { get; private set; } } As you can see in the code above, that DomainObject has a constructor expressing the dependency on a IDataProvider<T> . In my Ninject module, here

Generics, Covariance/Contravariance, etc

放肆的年华 提交于 2020-01-05 06:53:51
问题 So I have some Java code that makes extensive use of generics that compiles just fine. I ported it over to C# as follows: interface IFoo1 { } interface IFoo2 { } interface IBar<T, K> where T : IFoo1 where K : IFoo2 { List<T> GetFoo1s(); void AddAFoo1(T foo1); List<K> GetFoo2s(); void AddAFoo2(K foo2); } interface IBlip<T> where T : IBar<IFoo1, IFoo2> { T DoBlip(string input); void DoBlip2(T input); } interface IConverter<T, K> where T : IBar<IFoo1, IFoo2> where K : IBar<IFoo1, IFoo2> { K