reflection

How do I use reflection to call a generic method?

好久不见. 提交于 2021-02-05 11:48:08
问题 What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod<T>() using the Type stored in the myType variable? public class Sample { public void Example(string typeName) { Type myType = FindType(typeName); // What goes here to call GenericMethod<T>()? GenericMethod<myType>(); // This doesn't

How do I use reflection to call a generic method?

久未见 提交于 2021-02-05 11:47:15
问题 What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke GenericMethod<T>() using the Type stored in the myType variable? public class Sample { public void Example(string typeName) { Type myType = FindType(typeName); // What goes here to call GenericMethod<T>()? GenericMethod<myType>(); // This doesn't

Calling a method from Annotation using reflection

大城市里の小女人 提交于 2021-02-05 11:25:26
问题 I have Sample class with Size annotation case class Sample( attr: SomeTypeA @Size(value = 50) name: SomeTypeB) This Size annotation is a class that implements AnnotationInterface trait AnnotationInterface[T] { def getValue: T } class Size(value: Int) extends StaticAnnotation with AnnotationInterface[Int] { override def getValue: Int = value } And I have Extractor which is responsible for extracting class members using reflection class Extractor[A](implicit tt: TypeTag[A], ct: ClassTag[A] ) {

How to get all string values from action arguments in Web API C# method

白昼怎懂夜的黑 提交于 2021-02-05 10:54:26
问题 How to get all string values from action arguments in Web API C# method I have used this code: public override void OnActionExecuting(HttpActionContext actionContext) { Dictionary<string, object> list = actionContext.ActionArguments; for (int index = 0; index < list.Count; index++) { // need to all variables if the data type is string only // input parameter might be list or model or int or string list.ElementAt(index).Value; } } I need to write generic method to validate all input parameters

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

I can use org.reflection to get package classes using wildcard?

蓝咒 提交于 2021-02-05 09:27:47
问题 I was using Reflections reflections = new Reflections("com.mypackage.root", new MethodAnnotationsScanner()); But with this aproach i get back a lot of annotations in packages level that i don't need... What i really want for, is something like this, using an wildcard to get back the deep package level only. Reflections reflections = new Reflections("com.mypackage.root.*.deep", new MethodAnnotationsScanner()) Because the methods that i really need is in ".deep" package level... I try a lot of

C# Reflection - How to reload a class on runtime?

可紊 提交于 2021-02-05 09:13:06
问题 Currently I am working on a project in C# where I have to implement reflection. I have created a WPF application with a GUI. This GUI contains a combobox which contains all the classnames that implement a specific interface. The classes with the displayed classnames live in the same solution. Next to the combobox is a button to refresh the content in the combobox. However, when I run my application, modify a classname that implements the interface, and click on that refresh button the changes

C# Reflection - How to reload a class on runtime?

…衆ロ難τιáo~ 提交于 2021-02-05 09:12:06
问题 Currently I am working on a project in C# where I have to implement reflection. I have created a WPF application with a GUI. This GUI contains a combobox which contains all the classnames that implement a specific interface. The classes with the displayed classnames live in the same solution. Next to the combobox is a button to refresh the content in the combobox. However, when I run my application, modify a classname that implements the interface, and click on that refresh button the changes

How to get a function's signature as string in go

﹥>﹥吖頭↗ 提交于 2021-02-05 07:14:45
问题 I'm implementing a go module that loads go plugins. I'm assuming a function with a certain name and a certain signature exists on the main package, and would like to have a nice error message in case it is not found or not matching the expected signature. Given a variable with a function type, how can one get the underlying signature of that function? The following only prints the type's name (e.g. main.ModuleInitFunc ) and not the full signature. package main import "fmt" type ModuleInitFunc