generics

Reflection and Generics get value of property

社会主义新天地 提交于 2020-01-14 17:51:10
问题 i am trying to implement a generic method which allows to output csv file public static void WriteToCsv<T>(List<T> list) where T : new() { const string attachment = "attachment; filename=PersonList.csv"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "text/csv"; HttpContext.Current.Response

Type checking with generics

折月煮酒 提交于 2020-01-14 15:01:31
问题 Here's an illustrational class: class TypeChecker<T> { boolean isGood(Object something) { // won't compile return (something instanceof T); // maybe works, but oh so ugly! try { @SuppressWarnings("unchecked") T tmp = ((T) something); }catch(ClassCastException e) { return false; } return true; } } Is there any nice way to do this? The particular purpose is a bit different than in the example, but the idea is the same - to check if a variable of type T (parameter) can hold certain object. 回答1:

Type checking with generics

淺唱寂寞╮ 提交于 2020-01-14 15:01:11
问题 Here's an illustrational class: class TypeChecker<T> { boolean isGood(Object something) { // won't compile return (something instanceof T); // maybe works, but oh so ugly! try { @SuppressWarnings("unchecked") T tmp = ((T) something); }catch(ClassCastException e) { return false; } return true; } } Is there any nice way to do this? The particular purpose is a bit different than in the example, but the idea is the same - to check if a variable of type T (parameter) can hold certain object. 回答1:

How to pass Generic Type parameter to lambda expression?

自作多情 提交于 2020-01-14 14:44:32
问题 I have a lambda expression which accepts, a int? (nullable integer), which returns value if value exists or DBNull.Value otherwise. Func<int?, object> getId = id => id.HasValue ? id.Value : (object)DBNull.Value; The goal here is that, I want to make that expression slightly a bit more generic so that I can pass any nullable types like, DateTime? So here is a non-functional code I was starting off with, but not sure where to specify nullable's type . int? imageId; DateTime? actionDate; Func

How to make a generic type argument required in typescript?

…衆ロ難τιáo~ 提交于 2020-01-14 13:57:34
问题 How can I make a generic template type argument required? So far, the only way I found to do it is using never but it causes an error to happen at a different place other than the callsite of the generic. The TypeScript Playground example pasted here: type RequestType = | 'foo' | 'bar' | 'baz' interface SomeRequest { id: string type: RequestType sessionId: string bucket: string params: Array<any> } type ResponseResult = string | number | boolean async function sendWorkRequest<T extends

c# Generic on variable declaration, is this possible?

旧街凉风 提交于 2020-01-14 12:49:30
问题 I have the following problem: public class MyClass<T> where T : class { private MyOtherClass<T, U> variable where U : class; ... content ... } public class MyOtherClass<T, U> where T : class where U : class { ... content ... } Is that possible somehow? 回答1: If you want to make the type of a field or property of MyClass generic based on some type parameter U , you have to declare that as a type parameter of MyClass : public class MyClass<T, U> where T : class where U : class { private

c# Generic on variable declaration, is this possible?

拟墨画扇 提交于 2020-01-14 12:49:06
问题 I have the following problem: public class MyClass<T> where T : class { private MyOtherClass<T, U> variable where U : class; ... content ... } public class MyOtherClass<T, U> where T : class where U : class { ... content ... } Is that possible somehow? 回答1: If you want to make the type of a field or property of MyClass generic based on some type parameter U , you have to declare that as a type parameter of MyClass : public class MyClass<T, U> where T : class where U : class { private

Tricky question about generics, inheritance and chaining

戏子无情 提交于 2020-01-14 10:25:11
问题 For context - read this. Problem: class Program { static void Main() { var b = new bar(); b.buzz().fizz().buzz().fizz(); //cool // ^FAIL!!!...<------------------------------------ Console.ReadLine(); } } public class foo { public foo fizz() { return this; } } public class bar : foo { public bar buzz() { return this; } } Solution: class Program { static void Main() { var b = new bar(); b.buzz().fizz().buzz().fizz(); //cool stuff Console.ReadKey(); } } public static class fooExtensions { public

Generalising a “next permutation” function

删除回忆录丶 提交于 2020-01-14 09:36:10
问题 Below is an implementation of a function that returns the lexographically next permutation. This is useful in one of the Euler problems. It's written to work on Strings (which I needed for that). However, it should work on any indexed sequence of comparable values. I've tried generalising it by changing the two occurrences of String to IndexedSeq[Char], but this gets an error: euler-lib.scala:26: error: type mismatch; found : IndexedSeq[Char] required: String ((n.slice(pivot+1, successor):+ n

How to use generics to pass argument to a non-generic method?

亡梦爱人 提交于 2020-01-14 09:15:30
问题 Why does the following code not compile? How can I create a generic method which calls the appropriate "BitConverter.GetBytes" overload based on if the generic type is an "int", "bool", "char", etc.? More generally, how can I create a generic method which calls a non-generic method based on the generic parameter's type? using System; public class Test { public static void Main() { var f = new Foo(); f.GetBytes(10); // should call BitConverter.GetBytes(int); f.GetBytes(true); // should call