generic-type-argument

Access class property passed as generic type

情到浓时终转凉″ 提交于 2021-02-05 08:59:05
问题 I have two classes, which are passed to Serialization method and I would like to access two properties of these classes in Serialization method. The problem is that Serialization method parameter are passed as generic type and I do not know how to access properties of passed class in this case. The example below. public class MyClass1 { public string MyProperty1 { get; set; } //These properties are shared in both classes public bool Result { get; set; } public string EngineErrorMessage { get;

Detect if a generic type is open?

柔情痞子 提交于 2020-02-21 13:31:28
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

谁说胖子不能爱 提交于 2020-02-21 13:26:32
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

断了今生、忘了曾经 提交于 2020-02-21 13:26:14
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

心已入冬 提交于 2020-02-21 13:26:06
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

Detect if a generic type is open?

橙三吉。 提交于 2020-02-21 13:25:51
问题 I have a bunch of regular, closed and opened types in my assembly. I have a query that I'm trying to rule out the open types from it class Foo { } // a regular type class Bar<T, U> { } // an open type class Moo : Bar<int, string> { } // a closed type var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => ???); types.Foreach(t => ConsoleWriteLine(t.Name)); // should *not* output "Bar`2" Upon debugging the generic arguments of an open type, I found that their FullName is null (as

How to pass a type to generic method in Kotlin?

孤街浪徒 提交于 2020-02-02 04:13:09
问题 I have a generic method like below private fun <T> getSomething(): T { return "something" as T } How can I call this method with a variable T type? val types = arrayListOf<Type>(String::class.java, Boolean::class.java) types.forEach { type -> val something = getSomething<type>() // Unresolved reference: type } At runtime, I don't know what would be generic type T . I am getting the type from types and should pass it with generic getSomething method. Use case I want to call database which has

Confusion over Java generic method type inference

感情迁移 提交于 2020-01-16 05:13:10
问题 The sample method is given below: static <T> void doSomething(List<? super T> list1, List<? extends T> list2) { } I am wondering what type will be inferred in this situation and by what logic. Let's say I am calling this method like this: doSomething(new ArrayList<Object>(), new ArrayList<String>()); Would T type evaluate as Object or String ? 回答1: This call does not bind T to a specific class. Java does not need to know the exact T because of type erasure implementation of the generics. As

Confusion over Java generic method type inference

早过忘川 提交于 2020-01-16 05:13:09
问题 The sample method is given below: static <T> void doSomething(List<? super T> list1, List<? extends T> list2) { } I am wondering what type will be inferred in this situation and by what logic. Let's say I am calling this method like this: doSomething(new ArrayList<Object>(), new ArrayList<String>()); Would T type evaluate as Object or String ? 回答1: This call does not bind T to a specific class. Java does not need to know the exact T because of type erasure implementation of the generics. As

dot net core custom model binding for generic type parameters in mvc action methods

ⅰ亾dé卋堺 提交于 2020-01-05 05:51:53
问题 I am building a simple search, sort, page feature. I have attached the code below. Below are the usecases: My goal is to pass the "current filters" via each request to persist them particularly while sorting and paging. Instead of polluting my action method with many (if not too many) parameters, I am thinking to use a generic type parameter that holds the current filters. I need a custom model binder that can be able to achieve this. Could someone please post an example implementation? PS: I