generics

Generic Factory method to instantiate a derived class from the base class

不羁岁月 提交于 2021-02-07 20:02:14
问题 I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg. public abstract class ServiceClientBase : IServiceClient { } public abstract class Channel : ServiceClientBase { } public class ChannelImpl : Channel { } public class ServiceClientFactory { public T GetService<T>() where T : class, IServiceClient { // Use reflection to create the derived type instance T instance = typeof(T)

Generic Factory method to instantiate a derived class from the base class

落花浮王杯 提交于 2021-02-07 20:01:09
问题 I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg. public abstract class ServiceClientBase : IServiceClient { } public abstract class Channel : ServiceClientBase { } public class ChannelImpl : Channel { } public class ServiceClientFactory { public T GetService<T>() where T : class, IServiceClient { // Use reflection to create the derived type instance T instance = typeof(T)

Generic Factory method to instantiate a derived class from the base class

谁说我不能喝 提交于 2021-02-07 19:59:36
问题 I am in the process of creating a factory method which uses a generic abstract type parameter to return the instance of the concrete derived type using reflection. For eg. public abstract class ServiceClientBase : IServiceClient { } public abstract class Channel : ServiceClientBase { } public class ChannelImpl : Channel { } public class ServiceClientFactory { public T GetService<T>() where T : class, IServiceClient { // Use reflection to create the derived type instance T instance = typeof(T)

inference variable K has incompatible bounds

断了今生、忘了曾经 提交于 2021-02-07 19:40:47
问题 I was trying to write a solution for the problem stated here as Stuart Marks pointed out to use a helper class. I got stuck on the code due to this error: Test.java:27: error: incompatible types: inference variable K has incompatible bounds .collect(Collectors.groupingBy(p -> p.getT2())); ^ equality constraints: Tuple lower bounds: Integer where K,T are type-variables: K extends Object declared in method <T,K>groupingBy(Function<? super T,? extends K>) T extends Object declared in method <T,K

Page with type parameter

北战南征 提交于 2021-02-07 19:27:46
问题 I would like to use new feature of UWP -> x:Bind. In order to that, all my pages need to have ViewModel property (as described in tutorials). To avoid code duplicity, I have established base class as follows: public abstract class BasePage<TBaseVM> : Page, where TBaseVM : BaseVM { public TBaseVM VM { get; private set; } protected BasePage() { DataContextChanged += (s, e) => VM = e.NewValue as TBaseVM; } } As you can see this BasePage class contains property called "VM" and property is of type

Java generic class and wildcards

跟風遠走 提交于 2021-02-07 19:14:46
问题 I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass<C extends AbstractItem> { private C item; public void setItem(C item) { this.item = item; } public C getItem() { return item; } } An implementation of this class can be: public class MyChildClass extends MyMotherClass<ConcreteItem> { } ConcreteItem is just a simple class that extends AbstractItem (which is abstract). so MyChildClass have a ConcreteItem and I can use: MyChildClass child =

Using a generic parameter on overloaded methods

随声附和 提交于 2021-02-07 19:12:00
问题 Why does this program output Generic Value and not Hello world! : using System; class Example { public static void Print<T>(T value) { Console.WriteLine("Generic Value"); } public static void Print(string value) { Console.WriteLine(value); } public static void GenericFunc<T>(T value) { Print(value); } static void Main() { GenericFunc("Hello world!"); } } How is the generic method parameter being translated under the hood of C#? 回答1: Overload resolution is only done at compile-time. Since

Java generic class and wildcards

﹥>﹥吖頭↗ 提交于 2021-02-07 19:11:57
问题 I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass<C extends AbstractItem> { private C item; public void setItem(C item) { this.item = item; } public C getItem() { return item; } } An implementation of this class can be: public class MyChildClass extends MyMotherClass<ConcreteItem> { } ConcreteItem is just a simple class that extends AbstractItem (which is abstract). so MyChildClass have a ConcreteItem and I can use: MyChildClass child =

Java generic class and wildcards

喜欢而已 提交于 2021-02-07 19:04:47
问题 I've got a problem with generic classes in java. I've got this class: public abstract class MyMotherClass<C extends AbstractItem> { private C item; public void setItem(C item) { this.item = item; } public C getItem() { return item; } } An implementation of this class can be: public class MyChildClass extends MyMotherClass<ConcreteItem> { } ConcreteItem is just a simple class that extends AbstractItem (which is abstract). so MyChildClass have a ConcreteItem and I can use: MyChildClass child =

Handle Generic methods in a dictionary according to type

落花浮王杯 提交于 2021-02-07 18:27:26
问题 I have a generic method public delegate void Handler<T>(T val); I enable users to register to events and provide this delegate. I need to save the list of delegate according to their types. I tried saving in a dictionary of Type and object. when adding the method I cast it to a List<Handler<T>> according to the T. but then when an event occurred I do not have the T so cannot cast to the relevant list of generic handlers (I do have the Type but not the T) I solved this by saving methodInfo