generic-method

C# Generic .Contains() method implementing SqlFunctions.StringConvert in Entity Framework

好久不见. 提交于 2019-12-23 04:47:26
问题 I have a generic method which dynamically creates a query in Entity Framework. I use this as a search function on data table headers. The function works perfectly if the Entity property type/SQL data type is a string. This is because of the .Contains() extensions. The problem comes in when the data type is something other than a string. These data types don't have the .Contains() extension. I would like to be able to use this method across all data types, and have found that I could possibly

C# call Generic method dynamically [duplicate]

笑着哭i 提交于 2019-12-20 07:14:22
问题 This question already has answers here : How do I use reflection to call a generic method? (7 answers) Closed 5 years ago . Given the following Interfaces: interface IEntity { int Id{get;} } interface IPerson : IEntity { string Name{get;} int Age{get;} } interface ITeacher : IPerson { string StaffId{get;} } interface IStudent : IPerson { string StudentId{get;} string Courses{get;} } interface IRepository { T Get<T>(int id) where T : IEntity } I have the following classes in my namespace

C# Generic DateTime.ToString() with custom format [closed]

…衆ロ難τιáo~ 提交于 2019-12-13 14:15:31
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . When using: DateTime.ToString().Contains("2016") Entity Framework produces: CAST(DateValue AS nvarchar(max)) LIKE '%2016%' This uses the default date-format "mon dd yyyy hh:miAM (or PM)" I would like to user "yyyy-mm-dd hh:mi:ss (24h)" which is obtainable with something like: CONVERT(VARCHAR(max),

Java method with generic return Type

北战南征 提交于 2019-12-13 03:41:25
问题 Is there a way in Java to return different types with one declaration of a method? public Object loadSerialized(String path) { Object tmpObject; try { FileInputStream fis = new FileInputStream(path); ObjectInputStream ois = new ObjectInputStream(fis); tmpObject = (Object) ois.readObject(); ois.close(); fis.close(); return tmpObject; } catch (FileNotFoundException e) { return null; } catch (Exception e) { } } I want this method to return an Object and I cloud cast it to the right type at the

Assigning List of Integer Into a List of String

拥有回忆 提交于 2019-12-09 14:00:03
问题 I was learning Generics in Java and I came close to a very interesting piece of code. I know in Java it is illegal to add list of one type to another. List<Integer> integerList = new ArrayList<Integer>(); List<String> stringList=integerList; So in the second line I get a compile time error. But if I create a generic method inside a class like this, class GenericClass <E>{ void genericFunction(List<String> stringList) { stringList.add("foo"); } // some other code } And in the main class call

Java generics - Why “incompatible types” compilation error if a class' generic type doesn't exist in an invoked method? [duplicate]

蓝咒 提交于 2019-12-06 07:44:50
问题 This question already has answers here : What is a raw type and why shouldn't we use it? (15 answers) Closed 12 months ago . Please notice the code below doesn't compile, failing on the method result assignment: String s = a.method("abc"); . The compilation error: incompatible types: java.lang.Object cannot be converted to java.lang.String But, when changing A a to A<?> a or to A<Object> a , the compilation passes. * Please notice that the type <T> in the method is different than the type <O>

C#: How to use generic method with “out” variable

半腔热情 提交于 2019-12-05 21:36:50
问题 I want to create a simple generic function void Assign<T>(out T result) { Type type = typeof(T); if (type.Name == "String") { // result = "hello"; } else if (type.Name == "Int32") { // result = 100; } else result = default(T); } Usage: int value; string text; Assign(value); // <<< should set value to 100 Assign(text); // <<< should set text to "hello" My question is how do you program the code to set these values ie. the missing codes in comment section. Thanks for any help. 回答1: It looks

Java generics - Why “incompatible types” compilation error if a class' generic type doesn't exist in an invoked method? [duplicate]

让人想犯罪 __ 提交于 2019-12-04 15:52:35
This question already has answers here : What is a raw type and why shouldn't we use it? (15 answers) Closed 11 months ago . Please notice the code below doesn't compile, failing on the method result assignment: String s = a.method("abc"); . The compilation error: incompatible types: java.lang.Object cannot be converted to java.lang.String But, when changing A a to A<?> a or to A<Object> a , the compilation passes. * Please notice that the type <T> in the method is different than the type <O> in the class. Any idea what the compilation error? Also, why the generic definition in variable a

C#: How to use generic method with “out” variable

ぃ、小莉子 提交于 2019-12-04 04:58:20
I want to create a simple generic function void Assign<T>(out T result) { Type type = typeof(T); if (type.Name == "String") { // result = "hello"; } else if (type.Name == "Int32") { // result = 100; } else result = default(T); } Usage: int value; string text; Assign(value); // <<< should set value to 100 Assign(text); // <<< should set text to "hello" My question is how do you program the code to set these values ie. the missing codes in comment section. Thanks for any help. It looks like in this case maybe you're doing it to try to avoid boxing? Difficult to say without more information, but

How to convert List<T> to Array t[] (for primitive types) using generic-method?

爷,独闯天下 提交于 2019-12-02 00:24:06
问题 I am doing some tests with generic-methods and I'd like to transform these two methods below (convertFloatListToArray and convertShortListToArray) in just one (convertListToArray): public class Helper{ public static float[] convertFloatListToArray(List<Float> list){ float[] array = new float[list.size()]; for(int i = 0; i<list.size(); i++){ array[i] = list.get(i); } return array; } public static short[] convertShortListToArray(List<Short> list){ short[] array = new short[list.size()]; for(int