generics

Adding generic extension methods to interfaces like IEnumerable

走远了吗. 提交于 2020-01-14 08:45:47
问题 I've been trying and trying to make my generic extension methods work, but they just refuse to and I can't figure out why . This thread didn't help me, although it should. Of course I've looked up how to, everywhere I see they say it's simple and it should be in this syntax: (On some places I read that I need to add "where T: [type]" after the parameter decleration, but my VS2010 just says that's a syntax error.) using System.Collections.Generic; using System.ComponentModel; public static

VS Designer error: GenericArguments[0], 'X' on 'Y' violates the constraint of type parameter 'Z'

佐手、 提交于 2020-01-14 08:08:12
问题 I am trying to create forms that inherit from a "generic" base class where the generic argument of that base class has a constraint that it must implement one of my interfaces. It compiles and runs just fine. My problem is with the Visual Studio designer. It will open the form fine until I rebuild the project, after which it then reports the error below when trying to view the form designer until such time as I either restart Visual Studio, or remove the custom interface constraint in the

Java template function

自闭症网瘾萝莉.ら 提交于 2020-01-14 08:05:54
问题 I have a function that sometimes has to return a Date other times a DateTime (Joda-Time). static public <T extends Object> T convertTimeForServer(DateTime toSave) { DateTime temp = null; try { temp = toSave.withZone(DateTimeZone.forID(getServerTimeZone())); } catch (Exception e) { } T toReturn = null; if (toReturn.getClass().equals(temp)) { return (T) temp;//Return DATETIME } else { return (T) temp.toDate();//Return DATE } } Is it the right approach? How to use it? like this (timerHelper is

Binding a Generic List of type struct to a Repeater

六眼飞鱼酱① 提交于 2020-01-14 08:00:32
问题 I've had a bit of a problem trying to bind a generic list to a repeater. The type used in the generic list is actually a struct. I've built a basic example below: struct Fruit { public string FruitName; public string Price; // string for simplicity. } protected void Page_Load(object sender, EventArgs e) { List<Fruit> FruitList = new List<Fruit>(); // create an apple and orange Fruit struct and add to List<Fruit>. Fruit apple = new Fruit(); apple.FruitName = "Apple"; apple.Price = "3.99";

How to get Method Name of Generic Func<T> passed into Method

寵の児 提交于 2020-01-14 07:55:12
问题 I'm trying to get the method name of a function passed into an object using a .Net closure like this: Method Signature public IEnumerable<T> GetData<T>(Func<IEnumerable<T>> WebServiceCallback) where T : class { // either gets me '<LoadData>b__3' var a = nrdsWebServiceCallback.Method.Name; var b = nrdsWebServiceCallback.GetInvocationList(); return WebServiceCallback(); } I'm calling it like this: SessionStateService.Labs = CacheManager.GetData(() => WCFService.GetLabs(SessionStateService.var1,

How to use a swift class with a generic type in objective c

时光怂恿深爱的人放手 提交于 2020-01-14 07:41:07
问题 I have the following swift class with a NumericType T. @objc class MathStatistics<T: NumericType> : NSObject { var numbers = [T]() func sum() -> T? { if numbers.count == 0 { return nil } var sum = T(0) for value in numbers { sum = sum + value } return sum } } In swift a initialize the class object as follows: let statistics = MathStatistics<Double>() How do I initialize the same object in Objective C? The following line does not set the numeric type T. MathStatistics *stats = [[MathStatistics

java Generics Wildcard

六月ゝ 毕业季﹏ 提交于 2020-01-14 07:40:09
问题 I have a question on the use of wildcards in Java's generic types: what is the basic difference between List<? extends Set> and List<T extends Set> ? When would I use either? 回答1: Two reasons: To avoid unnecessary casts: You have to use the T variant for cases like this: public <T extends Set> T firstOf(List<T> l) { return l.get(0); } With ? this would become: public Set firstOf2(List<? extends Set> l) { return l.get(0); } ...which doesn't give the same amount of information to the caller of

How to access static methods of generic types

岁酱吖の 提交于 2020-01-14 07:11:16
问题 public class BusinessObjects<O> where O : BusinessObject { void SomeMethod() { var s = O.MyStaticMethod(); // <- How to do this? } } public class BusinessObject { public static string MyStaticMethod() { return "blah"; } } Is there a correct object oriented approach to accomplishing this or will I need to resort to reflection? EDIT: I went too far in trying to oversimplify this for the question and left out an important point. MyStaticMethod uses reflection and needs the derived type to return

Java generic class, inner class using parameter of outer class

帅比萌擦擦* 提交于 2020-01-14 05:53:46
问题 I have outer generic class which has some inner class, for which I'd like to use the generic type of outer class. However, I don't understand how to use generic parameters correctly. Example: class Outer<E extends CharSequence>{ class Inner extends ArrayList<E>{ } void func() { ArrayList<CharSequence> al = new ArrayList<CharSequence>(); al.add("abc"); // OK CharSequence a = al.get(0); // OK Inner in = new Inner(); in.add("abc"); // Error: The method add(E) in the type ArrayList<E> is not

How to convert generic primitive types in rust? [duplicate]

自闭症网瘾萝莉.ら 提交于 2020-01-14 03:55:15
问题 This question already has an answer here : How to cast generic types that I know to be integers? (1 answer) Closed 9 months ago . I would like to write something like the following in rust: pub struct Point<T> { pub x: T, pub y: T, } impl<T> Point<T> { pub fn from<U>(other: Point<U>) -> Point<T> { Point { x: other.x as T, y: other as T, } } } This is not possible: error[E0605]: non-primitive cast: `U` as `T` --> src/lib.rs:9:16 | 9 | x: other.x as T, | ^^^^^^^^^^^^ | = note: an `as`