generics

Generic class with class-bound constraint cannot be parametrized by class-bound protocol

放肆的年华 提交于 2021-02-05 11:11:58
问题 I'd like to have a generic weak reference to an object and parametrize it by a protocol that is class-bound. Here is the code example that does not work in my case: protocol Protocol: class { // also written as `protocol Protocol: AnyObject {` func function() } final class A: Protocol { func function() {} } final class Weak<Type> where Type: AnyObject { final private weak var property: Type? init(property: Type) { self.property = property } } let a = A() let something = Weak<Protocol>

Generic class with class-bound constraint cannot be parametrized by class-bound protocol

冷暖自知 提交于 2021-02-05 11:06:04
问题 I'd like to have a generic weak reference to an object and parametrize it by a protocol that is class-bound. Here is the code example that does not work in my case: protocol Protocol: class { // also written as `protocol Protocol: AnyObject {` func function() } final class A: Protocol { func function() {} } final class Weak<Type> where Type: AnyObject { final private weak var property: Type? init(property: Type) { self.property = property } } let a = A() let something = Weak<Protocol>

C# Passing generic class as an event parameter

喜你入骨 提交于 2021-02-05 11:03:20
问题 I've been working with generics over the last few days and I encountered an issue by trying to pass generic types as parameters in events/delegates. A very friendly member of stack could bring some light into one question about generics I posted last Friday. However, I'd like to bring this up again, because I still don't understand how I am supposed to make it work correctly. In the following code you can see that I have a class "Catalog", which can raise an event, which contains a generic

Generic overridden method with subclass as return type

霸气de小男生 提交于 2021-02-05 10:49:04
问题 Let's say that i have a base class Animal. public abstract class Animal; This animal class has the abstract method: public abstract T Copy<T>() where T : Animal When this method is overridden in the Lion class: public class Lion : Animal { string roar = "Roar"; } i wish to return a copy of this lion without its references. So what i think it should be overridden like is this: public abstract T Copy<T>() { return new Lion(){ roar = this.roar; } } but this is not allowed, because Lion cannot be

How to get all types of the inner structures of a nested type?

微笑、不失礼 提交于 2021-02-05 09:46:26
问题 I have a function bellow, public void park( List<List< List< Integer>>> l){ System.out.println("park"); } and when I tried to use Method.getParameters() + Parameter.getParameterizedType(), I got java.util.List< java.util.List<java.util.List<java.lang.Integer>>>> But, I need to get the type of the inner side of the nested structure, say, java.util.List< java.util.List<>> and Integer . When I get that, I use getAnnotationsByType to get all annotations of the inner structures. I have thought to

How to get all types of the inner structures of a nested type?

心已入冬 提交于 2021-02-05 09:45:20
问题 I have a function bellow, public void park( List<List< List< Integer>>> l){ System.out.println("park"); } and when I tried to use Method.getParameters() + Parameter.getParameterizedType(), I got java.util.List< java.util.List<java.util.List<java.lang.Integer>>>> But, I need to get the type of the inner side of the nested structure, say, java.util.List< java.util.List<>> and Integer . When I get that, I use getAnnotationsByType to get all annotations of the inner structures. I have thought to

using covariance on an interface …Runtime Error Unable to cast object of type 'derviedAService ' to type 'iBaseService`1[mybase]'

女生的网名这么多〃 提交于 2021-02-05 09:43:24
问题 using System; using System.Collections.Generic; namespace mymodels { public abstract class mybase { public string v1 {get; set;} } public class derivedA : mybase { public string v2 {get; set;} } public class derivedB : mybase { public string v2 {get; set;} } } namespace myservices { interface iBaseService<T> where T : mymodels.mybase { T Get (); } public class derviedAService : iBaseService<mymodels.derivedA> { public derviedAService (){} public mymodels.derivedA Get() { mymodels.derivedA d =

using covariance on an interface …Runtime Error Unable to cast object of type 'derviedAService ' to type 'iBaseService`1[mybase]'

故事扮演 提交于 2021-02-05 09:39:59
问题 using System; using System.Collections.Generic; namespace mymodels { public abstract class mybase { public string v1 {get; set;} } public class derivedA : mybase { public string v2 {get; set;} } public class derivedB : mybase { public string v2 {get; set;} } } namespace myservices { interface iBaseService<T> where T : mymodels.mybase { T Get (); } public class derviedAService : iBaseService<mymodels.derivedA> { public derviedAService (){} public mymodels.derivedA Get() { mymodels.derivedA d =

Is it possible to use the generic type to determine runtime functionality?

情到浓时终转凉″ 提交于 2021-02-05 09:38:33
问题 I have a situation where I might have some code like this: const data = fetchSomeData(); //Can be anything const checkedData = checkDataIs<SomeSpecificType>(data); // now I can treat checkedData as SomeSpecificType That is, at compile time I know what shape the data should be, and want to write my code as such, have all the advantages of type checking, but at runtime I need to run a function first to check that. (And it'll throw an error if the data doesn't match). I know that I can achieve

Cast a property to its actual type dynamically using reflection (where actual type is generic)

我是研究僧i 提交于 2021-02-05 09:17:06
问题 This is a slightly different question asked here. I modified the same code into my needs like below: using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace cns01 { class Program { public class ClassA<T> { public int IntProperty { get; set; } = 999; } public class ClassB<T2> { public ClassA<int> MyIntProperty { get; set; } public ClassA<string> MyStringProperty { get; set; } } static void Main