()
| . , . . |
| . |
|
|
( , . reflection) , . , , . [1].
() . , , . , , . , , , , . , (, Smalltalk, ). / (, Java, , ML, Haskell).
|
|
[] -
- , - . - , . , - , . , , - , , . - , , . , , .
[]
Brian Cantwell Smith 1982 . [2] [3] meta-circular interpreter, 3-Lisp.
[]
. . .
. , , X Y . X Y . - , X Y; : X, Y.
[]
, , , , . :
- (, , , . .) first-class object ;
- ;
- , ;
- .
. MOO . , , , . : callers() ; , callers()[1] .
, . Objective-C , , , . , , Common Lisp, .
, , .
[]
foo Foo hello ( Hello) . : , .
[] C#
// new Foo().Hello(); // Type type=System.Type.GetType("Foo"); var foo=Activator.CreateInstance(type); foo.GetType().GetMethod("Hello").Invoke(foo, null);
[] ECMAScript
// new Foo().hello() // // assuming that Foo resides in this new this['Foo']()['hello']() // or without assumption new (eval('Foo'))()['hello']()
[] Java
// new Foo().hello(); // Class cls = Class.forName("Foo"); cls.getMethod("hello", null).invoke(cls.newInstance(), null);
[] Qt/C++
Qt C++ / Qt QMetaObject, Qt.
QObject *obj = new QPushButton; obj->metaObject()->className(); // "QPushButton" QPushButton::staticMetaObject.className(); // "QPushButton"
[] Lua
-- Foo.hello() -- _G['Foo']['hello']()
[] Objective-C
// Foo *foo = [[Foo alloc] init]; [foo hello]; // Class cls = NSClassFromString(@"Foo"); id foo = [[cls alloc] init]; SEL selector = NSSelectorFromString(@"hello"); [foo performSelector:selector withObject:nil];
[] Perl
# my $foo = Foo->new(); $foo->hello(); # my $class = "Foo"; my $method = "hello"; my $object = $class->new(); $object->$method();
[] PHP
// $oFoo = new Foo(); $oFoo->hello(); // $oReflector = new ReflectionClass('Foo'); $oFoo = $oReflector->newInstance(); $oHello = $oReflector->getMethod('hello'); $oHello->invoke($oFoo); // callback $oFoo = new Foo(); call_user_func(array($oFoo,'hello')); // $class_name = "Foo"; $f = new $class_name(); $method = "hello"; $f->$method();
[] Python
# Foo().hello() # getattr(globals()['Foo'](), 'hello')()
[] Ruby
# Foo.new.hello # Object.const_get(:Foo).send(:new).send(:hello)
[] Smalltalk
" " Foo new hello " " ((Smalltalk at: #Foo) perform: #new) perform: #hello
[] Io
Foo := Object clone do( hello := method( "Hello" println ) ) # Foo hello # getSlot("Foo") getSlot("hello") call
[] ActionScript 3.0
// var foo:Foo = new Foo(); foo.hello(); // var cls:Object = getDefinitionByName("Foo"); var foo:Object = new cls(); foo.hello();
[] Delphi 2010
// var foo : TFoo; begin foo := TFoo.Create(); foo.Hello(); end; // var c : TRttiContext; t : TRttiInstanceType; foo : TValue; begin c := TRttiContext.Create; t := (c.FindType('TFoo') as TRttiInstanceType); foo := t.GetMethod('Create').Invoke(t.MetaclassType,[]); t.GetMethod('Hello').Invoke(foo,[]); c.Free; end.
[] .
[]
Notes
- . Matt Hurlbutt (.)
- , , " ", , , 1982 (.)
- Brian C. Smith. . MIT-LCS-TR-272, , , Mass., 1982 (.)
Documents
- Jonathan M. Sobel and Daniel P. Friedman. An Introduction to Reflection-Oriented Programming (1996), Indiana University.
[]
- Ira R. Forman and Nate Forman, Java Reflection in Action (2005), ISBN 1-932394-18-4
- Ira R. Forman and Scott Danforth, Putting Metaclasses to Work (1999), ISBN 0-201-43305-2
[]
- Reflection in logic, functional and object-oriented programming: a short comparative study
- An Introduction to Reflection-Oriented Programming
- Brian Foote's pages on Reflection in Smalltalk
- Java Reflection Tutorial from Sun Microsystems
| . , . |
