reflection

How to instantiate class by it's string name in Python from CURRENT file? [duplicate]

Deadly 提交于 2020-05-15 02:41:10
问题 This question already has answers here : Instantiate a Python class from a name [duplicate] (6 answers) Closed 2 years ago . Suppose I have myfile.py with some classes A , B and C defined INSIDE it. Now I want to instantiate class by it's name in str. I don't understand what to pass to getattr in order to do this. All examples like this assume that classes are in other module: module = __import__(module_name) class_ = getattr(module, class_name) instance = class_() but I don't have module

Force Uniform Constructor and Method Annotation Values?

做~自己de王妃 提交于 2020-05-14 10:39:30
问题 I'm writing a custom API using Reflection to save Objects to file. I have the following class structure: @Constructor public XYZPOJO(@Key(key = "word") String word, @Key(key = "variations") ArrayList<String> varList) { this.word = word; this.varList = varList; } String word; ArrayList<String> varList = new ArrayList<String>(); @Key(key = "word") public String getWord() { return word; } @Key(key = "variations") public ArrayList<String> getVarList() { return varList; } When saving Object to

Invoking GetMethod() on a System.__ComObject always returns null

时光毁灭记忆、已成空白 提交于 2020-05-13 17:43:11
问题 I am using .NET 4.0 and dynamic to invoke members on a System.__ComObject at runtime. I instanciate the object in the following way: dynamic DrApi; DrApi = Activator.CreateInstance(SprImportedTypes.DrApi); The types are declared in a static class like this: internal static Type DrApi = Type.GetTypeFromProgID("DrApi.DrApi.1"); Since the object is dynamic, I can invoke methods without any difficulty: string vers = string.Empty; DrApi.Version(ref vers); For consolidation and localized error

C# AppDomain: Execute assembly from entry point

亡梦爱人 提交于 2020-05-13 14:56:28
问题 I am trying to create a Windows sandbox application, building upon the "How to" found here: "https://msdn.microsoft.com/en-us/library/bb763046(v=vs.110).aspx" In the example it loads a specific type from a DLL whereas I would like to be able to execute an assembly from its entry point with restricted permissions. The program I am using for testing purposes is a simple hello world application. using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) {

Load assemblies with references from subfolders at runtime

白昼怎懂夜的黑 提交于 2020-05-13 06:02:45
问题 I am currently working on a project which is supposed to work as a framework for several Add-Ons, which should be loaded at runtime. I am tasked to have following structure in my application folder: 2 Directories with subfolders. One named " /addons " for Add-Ons and one named " /ref " for any additonal references these addons might use (like System.Windows.Interactivity.dll ) When selecting one of the Add-Ons from a menu in the applicaiton, the .dll is supposed to be loaded at runtime and a

Load assemblies with references from subfolders at runtime

一笑奈何 提交于 2020-05-13 05:59:32
问题 I am currently working on a project which is supposed to work as a framework for several Add-Ons, which should be loaded at runtime. I am tasked to have following structure in my application folder: 2 Directories with subfolders. One named " /addons " for Add-Ons and one named " /ref " for any additonal references these addons might use (like System.Windows.Interactivity.dll ) When selecting one of the Add-Ons from a menu in the applicaiton, the .dll is supposed to be loaded at runtime and a

Create BiConsumer from LambdaMetafactory

只谈情不闲聊 提交于 2020-05-12 11:10:23
问题 I'm trying to dynamically create a method reference of type BiConsumer through LambdaMetafactory. I was trying to apply two approaches found on https://www.cuba-platform.com/blog/think-twice-before-using-reflection/ - createVoidHandlerLambda and here Create BiConsumer as Field setter without reflection the Holger's answer. However in both cases I'm having below error: Exception in thread "main" java.lang.AbstractMethodError: Receiver class org.home.ref.App$$Lambda$15/0x0000000800066040 does

Create BiConsumer as Field setter without reflection

守給你的承諾、 提交于 2020-05-12 10:57:13
问题 I try to get the maximum performance in one of my scripts, without doing a major refactor. I spotted method that creates a BiConsumer from a Field using reflection. return (c, v) -> { try { field.set(c, v); } catch (final Throwable e) { throw new RuntimeException("Could not set field: " + field, e); } }; Reflection has the reputation of being slow. So I though I could use the method handles. Lookup lookup = MethodHandles.lookup(); MethodHandle mh = lookup.unreflectSetter(field); return (c, v)

Which ClassLoader should I supply to Proxy.newProxyInstance(…)?

好久不见. 提交于 2020-05-11 04:38:05
问题 I have read the documentation, but I still don't understand which classloader I should supply as an argument. I've tried a few options, but this seems to have no effect on compilation or the behavior of the proxy. It is a little unsettling that I can pass anything as the class loader argument, including null , and the code still works fine. Can anyone explain this, and tell me what kind of errors can arise if I provide a bad argument for the classloader? I should add that I don't really have

Loop through an object's properties and get the values for those of type DateTime

£可爱£侵袭症+ 提交于 2020-05-11 03:46:09
问题 I have a list of objects (Cars). For each car in the list I need to loop through it and find any properties of type DateTime . If I find a property of DateTime I need to get the value and do a time conversion. For now lets just print out the DateTime property value to the console. I am having issues understanding what I need to put in the first parameter of the prop.GetValue function. Any help would be appreciated! foreach (var car in carList) { foreach (PropertyInfo car in car.GetType()