introspection

C# “is” operator - is that reflection?

元气小坏坏 提交于 2019-11-29 01:41:33
问题 A colleague asked me an interesting question today - is the C# keyword/operator "is" considered reflection? object tmp = "a string"; if(tmp is String) { } How is this operator implemented behind the scenes? Does it require reflection or introspection? Or because of the strongly typed nature of the language, is the Type of the object immediately accessable as a top-level attribute of the object in memory? MSDN states that: Note that the is operator only considers reference conversions, boxing

Call private methods and private properties from outside a class in PHP

我的未来我决定 提交于 2019-11-29 01:28:59
I want to access private methods and variables from outside the classes in very rare specific cases. I've seen that this is not be possible although introspection is used. The specific case is the next one: I would like to have something like this: class Console { final public static function run() { while (TRUE != FALSE) { echo "\n> "; $command = trim(fgets(STDIN)); switch ($command) { case 'exit': case 'q': case 'quit': echo "OK+\n"; return; default: ob_start(); eval($command); $out = ob_get_contents(); ob_end_clean(); print("Command: $command"); print("Output:\n$out"); break; } } } } This

Generate SQL to update primary key

大城市里の小女人 提交于 2019-11-29 01:27:37
I want to change a primary key and all table rows which reference to this value. # table master master_id|name =============== foo|bar # table detail detail_id|master_id|name ======================== 1234|foo|blu If I give a script or function table=master, value-old=foo, value-new=abc I want to create a SQL snippet that executes updates on all tables which refere to table "master": update detail set master_id=value-new where master_id=value-new; ..... With the help of introspection, this should be possible. I use postgres. Update The problem is, that there are many tables which have a foreign

Any way to determine which object called a method?

人走茶凉 提交于 2019-11-28 23:11:53
I'm hoping that Ruby's message-passing infrastructure means there might be some clever trick for this. How do I determine the calling object -- which object called the method I'm currently in? As an option, there is a binding_of_caller gem that allows you to execute code in context of any caller on the call stack (caller, caller's caller and so on). It's useful for inspecting (read do anything at any position on the call stack ) call stack in development, as used in better_errors . Objects of class Binding encapsulate the execution context at some particular place in the code and retain this

Does Scala have introspection capable of something similar to Python's dir()?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 22:32:42
问题 Yes, I know it's considered lazy by the non-Pythonistas. The reason I ask is that documentation is still woefully lacking in many Scala libraries (e.g. Scala-dbc, but that's not all I'm looking at), and if I could see the attributes of an object/class at runtime, I could at least figure out what's available. Thanks. 回答1: Scala does not have a reflection API. The only way to access this information is to use the Java reflection API. This has the disadvantage that the structure may change as

Find functions explicitly defined in a module (python)

ぃ、小莉子 提交于 2019-11-28 21:16:43
Ok I know you can use the dir() method to list everything in a module, but is there any way to see only the functions that are defined in that module? For example, assume my module looks like this: from datetime import date, datetime def test(): return "This is a real method" Even if i use inspect() to filter out the builtins, I'm still left with anything that was imported. E.g I'll see: ['date', 'datetime', 'test'] Is there any way to exclude imports? Or another way to find out what's defined in a module? Are you looking for something like this? import sys, inspect def is_mod_function(mod,

How to list all fields of a class (and no methods)?

烈酒焚心 提交于 2019-11-28 21:03:14
Suppose o is a Python object, and I want all of the fields of o , without any methods or __stuff__ . How can this be done? I've tried things like: [f for f in dir(o) if not callable(f)] [f for f in dir(o) if not inspect.ismethod(f)] but these return the same as dir(o) , presumably because dir gives a list of strings. Also, things like __class__ would be returned here, even if I get this to work. You can get it via the __dict__ attribute, or the built-in vars function, which is just a shortcut: >>> class A(object): ... foobar = 42 ... def __init__(self): ... self.foo = 'baz' ... self.bar = 3 ..

What is the C# equivalent to Java's isInstance()?

若如初见. 提交于 2019-11-28 18:30:32
I know of is and as for instanceof , but what about the reflective isInstance() method? The equivalent of Java’s obj.getClass().isInstance(otherObj) in C# is as follows: bool result = obj.GetType().IsAssignableFrom(otherObj.GetType()); Note that while both Java and C# work on the runtime type object (Java java.lang.Class ≣ C# System.Type ) of an obj (via .getClass() vs .getType() ), Java’s isInstance takes an object as its argument, whereas C#’s IsAssignableFrom expects another System.Type object. bool result = (obj is MyClass); // Better than using 'as' Depends, use is if you don't want to

How do I find all the property keys of a KVC compliant Objective-C object?

十年热恋 提交于 2019-11-28 16:52:50
Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol? Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant object. Does such a method exist? I haven't found anything in searching the Apple docs so far. Thanks, G. oxigen #import "objc/runtime.h" unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for(i = 0; i < outCount; i++) { objc_property_t property = properties[i]; const char *propName = property_getName

How do you list the currently available objects in the current scope in ruby?

最后都变了- 提交于 2019-11-28 15:38:53
问题 I'm new to ruby and I'm playing around with the IRB. I found that I can list methods of an object using the ".methods" method, and that self.methods sort of give me what I want (similar to Python's dir( builtins )?), but how can I find the methods of a library/module I've loaded via include and require? irb(main):036:0* self.methods => ["irb_pop_binding", "inspect", "taguri", "irb_chws", "clone", "irb_pushws", "public_methods", "taguri=", "irb_pwws", "public", "display", "irb_require", "irb