برمجة انعكاسية

من أرابيكا، الموسوعة الحرة
اذهب إلى التنقل اذهب إلى البحث

في البرمجة وعلم الحاسوب البرمجة الإنعكاسية هي اختبار البرنامج وتحليله في أثناء تشغيله وتنفيذه.[1]

أمثلة

C#

المثال التالي في لغة C#:

// Without reflection
Foo foo = new Foo();
foo.PrintHello();

// With reflection
Object foo = Activator.CreateInstance("complete.classpath.and.Foo");
MethodInfo method = foo.GetType().GetMethod("PrintHello");
method.Invoke(foo, null);

جافا

المثال التالي في لغة جافا:

// Without reflection
Foo foo = new Foo();

foo.hello();

// With reflection
Object foo = Class.forName("complete.classpath.and.Foo").newInstance();
// Alternatively: Object foo = Foo.class.newInstance();
Method m = foo.getClass().getDeclaredMethod("hello", new Class<?>[0]);
m.invoke(foo);

PHP

المثال التالي في لغة بي إتش بي:

// Without reflection
$foo = new Foo();
$foo->hello();

// With reflection
$reflector = new ReflectionClass('Foo');
$foo = $reflector->newInstance();
$hello = $reflector->getMethod('hello');
$hello->invoke($foo);

// using callback
$foo = new Foo();
call_user_func(array($foo, 'hello'));

// using variable variables syntax
$className = 'Foo';
$foo = new $className();
$method = 'hello';
$foo->$method();

بايثون

المثال التالي في لغة بايثون:

# without reflection
obj = Foo()
obj.hello()

# with reflection
class_name = "Foo"
method = "hello"
obj = globals()[class_name]()
getattr(obj, method)()

# with eval
eval("Foo().hello()")

الاستخدام

يستخدم عادة لاختبار النظام وسير عمله كما يستخدم أيضا في رصد مكان الخلل في حالة وجوده ويمكن ملاحظة أن أكثر مستعملي هذه الخاصية هم القراصنة فهم يستخدمونها لرصد الثغرات

انظر أيضاً

مراجع

  1. ^ "معلومات عن انعكاس (برمجة) على موقع semanticscholar.org". semanticscholar.org. مؤرشف من الأصل في 2023-07-13.