reflection

Get generic argument type and value supplied to a generic method

梦想的初衷 提交于 2019-12-30 08:26:35
问题 How do you get the argument value supplied to a closed/constructed generic method? It's been a while since I haven't touched Reflection. All this used to be at the back of my, umm, whatever. class Program { static void Main(string[] args) { new ConcreteFoo().GenericMethod<int>(5); Console.ReadKey(); } } class ConcreteFoo { public void GenericMethod<Q>(Q q) { var method = MethodInfo.GetCurrentMethod(); var parameters = method.GetParameters(); if (parameters.Length > 0) foreach (var p in

Java : programmatically determine all of the package names loaded on the classpath

廉价感情. 提交于 2019-12-30 08:13:10
问题 Any suggestions as to how to approach how I would find out a list of package names that exist on the current classpath ? This needs to be done programmatically at run time by one of the classes loaded (and executing) on the classpath (i.e. inside out , not outside in). More details: One approach I considered was to use reflection on each of the classes loaded thus far by the class loader, and extract the package names from them. However, my application already runs into thousands of classes,

this.getClass().getFields().length; always returns 0 [duplicate]

北城以北 提交于 2019-12-30 08:12:03
问题 This question already has answers here : java reflection getFields for private member| accessing object name value dynamically (2 answers) Closed 6 years ago . I am trying to get the number of fields in a particular class. However the technique I am using is not working and always returns 0: this.getClass().getFields().length; How do I get the field count of a particular class? 回答1: Use this.getClass().getDeclaredFields().length The getFields method is for accessible public fields - see

Groovy, get enclosing function's name?

本秂侑毒 提交于 2019-12-30 08:10:24
问题 I'm using Groovy 1.8.4, trying to get the name of the enclosing function... def myFunction() { println functionName?? } I've tried delegate , this , owner , Groovy complains no such objects found. I also tried the Java hack new Exception().getStackTrace()[0].getMethodName() , but that just prints newInstance0 回答1: How about groovy's StackTraceUtils.sanitize? Here's a quick example: import org.codehaus.groovy.runtime.StackTraceUtils class A { def methodX() { methodY() } def methodY() { methodZ

How to load a UserControl in WPF with reflection?

夙愿已清 提交于 2019-12-30 07:33:13
问题 private void Window_Loaded(object sender, RoutedEventArgs e) { var assm = Assembly.LoadFrom("wpflib.dll"); foreach (var t in assm.GetTypes()) { var i = t.GetInterface("test.ILib"); if (i != null) { var tmp = Activator.CreateInstance(typeof(UserControl)) as UserControl; this.stackPanel1.Children.Add(tmp); } } } tmp(UserControl1 in wpflib.dll) only contains a label and a textbox. Windows1 (test.exe) reference ILib.dll, and only contains a stackPanel1. But, why there is nothong in Windows1

Postsharp: how does it work?

人盡茶涼 提交于 2019-12-30 07:05:08
问题 Following the advice got on another question of mine, I converted the code there quoted to be used with PostSharp: Attribute: [Serializable] public sealed class InitAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionEventArgs eventArgs) { Console.Write("Works!"); } } static class Logger { public static string _severity; public static void Init(string severity) { _severity = severity; } [Init()] public static void p() { Console.WriteLine(_severity); } } Still, I

Runtime resolution of type arguments using scala 2.10 reflection

有些话、适合烂在心里 提交于 2019-12-30 06:23:52
问题 Given a type declaration, I am able to resolve the type argument. scala> reflect.runtime.universe.typeOf[List[Int]] match {case x:TypeRef => x.args} res10: List[reflect.runtime.universe.Type] = List(Int) For a runtime value, The same method doesn't work. scala> reflect.runtime.currentMirror.reflect(List(42)).symbol.toType match {case x:TypeRef => x.args} res11: List[reflect.runtime.universe.Type] = List(B) Is there a way to overcome the type erasure for reflected values? 回答1: An example based

Java Generics - Class or Class<? extends SomeClass>

我的未来我决定 提交于 2019-12-30 06:06:46
问题 I am writing a program which will use Java reflection (i.e., Class.forName() ) to dynamically create class instance based on user's input. One requirement is that the instance my program creates must extend one specific Class I defined, called it SomeClass . My question is: for storing this class type, should I use bounded generic, Class<? extends SomeClass> , or simply unbounded generic, Class ? I found some Java books say that Class is one of the good practices for using unbounded wildcard

How to make sure controller and action exists before doing redirect, asp.net mvc3

不羁的心 提交于 2019-12-30 06:04:14
问题 In one of my controller+action pair, I am getting the values of another controller and action as strings from somewhere and I want to redirect my current action. Before making a redirect I want to make sure that controller+action exists in my app, if not then redirect to 404. I am looking for a way to do this. public ActionResult MyTestAction() { string controller = getFromSomewhere(); string action = getFromSomewhereToo(); /* At this point use reflection and make sure action and controller

How to get source file-name/line-number from a java.lang.Class object

梦想的初衷 提交于 2019-12-30 05:46:07
问题 Is it possible, given a java.lang.Class object, to get the source file name and the line number at which the class was declared? The data should be available in the .class file's debug info. The only place I know of, where the JDK returns such debug info is in java.lang.StackTraceElement but I'm not sure if it's possible to force Java to create a java.lang.StackTraceElement instance for an arbitrary class, because we are not executing a method in the class. My exact use case is an anonymous