reflection

Create instance of generic type in Java when parameterized type passes through hierarchies?

柔情痞子 提交于 2019-12-24 14:24:58
问题 I have been reading the answers to the question: Create instance of generic type in Java? I have implemented the approach suggested by Lars Bohl. I adapted his code as follows: import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; public class ParameterizedTypeEg<E> { public Class<E> getTypeParameterClass() { Type type = getClass().getGenericSuperclass(); ParameterizedType paramType = (ParameterizedType) type; return (Class<E>) paramType.getActualTypeArguments()[0]; }

MethodHandle to a getter/setter from another class gives a NoSuchFieldError

一世执手 提交于 2019-12-24 14:12:22
问题 Suppose I have simple javabean MyPerson with a name getter and setter: public class MyPerson { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } Now I am running this main code that simply gets and sets that name field: public static void main(String[] args) throws Throwable { MethodHandles.Lookup lookup = MethodHandles.lookup(); MethodHandle getterMethodHandle = lookup.findGetter(MyPerson.class, "name", String.class);

New instance of struct from the type at runtime in GO

廉价感情. 提交于 2019-12-24 13:49:10
问题 I am trying to create new instance of a struct, using it's type (reflect.TypeOf) at runtime. I have followed this thread on StackOverflow How do you create a new instance of a struct from it's Type at runtime in Go?. Here is my implementation (also at http://play.golang.org/p/BtX0d5Ytu8): package main import ( "fmt" "reflect" "encoding/json" "bytes" ) type UserInfo struct { Email string `json:"email"` FullName string `json:"name"` ID string `json:"_id"` } func main() { fmt.Println("Hello,

How do I retrieve a reference to a subobject using Reflection in C#?

南楼画角 提交于 2019-12-24 13:35:32
问题 I'm trying to use duck typing using reflection in C#. I have an object of some random type and I want to find if it implements an interface with a specific name and if it does - retrieve a reference to that interface subobject so that I can later read (get) a property value via that interface. Effectively I need as using Reflection. The first part is easy var interfaceOfInterest = randomObject.GetType().GetInterface("Full.Interface.Name.Here"); which will either retrieve the interface

Can one get/set class-level members using Enhanced RTTI in Delphi?

ぐ巨炮叔叔 提交于 2019-12-24 13:10:59
问题 Preface, it seems i failed to say it clear. I want to enumerate, read and set, all the class var or class properties of a given TClass variable. There is no problem in finding TClass - it is passed. There is no problem in filtering by presence of given attribute. There problem is that RTTI just misses the way to enumerate class fields rather than instance fields. I wanted to make a declarative DLL Loader. Since the DLLs are process-global in windows (you cannot load the same DLL twice) it

C++ Make a Program Write Over Itself

ぐ巨炮叔叔 提交于 2019-12-24 12:57:35
问题 I posted a question on a similar topic a couple days ago (and one a couple years ago), but I decided to go ahead and get started. I am trying to inject C++ code into C++ code (in a somewhat portable manner using no os specific features and trying to be compiler/toolchain independent manner). I basically want to do this in an attempt to do runtime C++ scripts. I wrote a small test program (its really just kinda thrown together and hacky): Main.cpp: #include <stdlib.h> #include <iostream>

How to figure out what protocols the type implements?

筅森魡賤 提交于 2019-12-24 12:44:16
问题 Given some type or record, how can I get all the protocols it implements? Let's say we have the following code: (defprotocol P1 (op1 [this])) (defprotocol P2 (op2 [this])) (defrecord R [] P1 (op1 [_] 1) P2 (op2 [_] 2)) And what I need is a function that does something like this: (all-protocols-for-type R) ;; => (P1 P2) It will be good if there's something backend-agnosting, because I'd like to have a mechanism for both Clojure and ClojureScript. UPD: the intention for this is to introspect

How can I rewrite this LINQ query with reflection

[亡魂溺海] 提交于 2019-12-24 12:33:37
问题 So I had written this LINQ query using reflection, and later found out it isn't supported. What would be the best way to get the same functionality from this code? List<Profile> profilesFromUUID = await MobileService.GetTable<Profile>().Where(p => typeof(Profile) .GetProperty(handler.Name + "UUID").GetValue(p) == obj.uuid).ToListAsync(); 回答1: Use the reflection to create the query, not in the query. Consider: public static IQueryable<Profile> Filter( this IQueryable<Profile> source, string

Dynamic variables with given type

妖精的绣舞 提交于 2019-12-24 12:16:11
问题 I have written a small class, which reads out annotation from methods. Now I want to extend that class to make it more dynamic. My class uses at the moment following code for reading out the annotation: ExtendedCommandAnnotation e = foo.getClass() .getAnnotation(ExtendedCommandAnnotation.class); String startTag = e.annoPropStartTag(); That is the simple case with fixed annotation. In the new version I haven't any fixed annotation. I will get the annotation 'ExtendedCommandAnnotation' in a

WPF form in a console app

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 12:12:57
问题 Im running a console app that loads a dll and calls a method from that dll that creates a WPF form. So I'm just calling to Program.Execute() method and it does all the creation of the form. All reflection business goes well, but the form does not appear. I've been told that this is because a console app does not have a windows message loop, but I'm sure there is a way to simulate that. For example, I tried playing with System.Windows.Threading.DispatcherFrame , but to no avail - the form