CSC/ECE 517 Fall 2009/wiki2 8 RP: Difference between revisions
Line 179: | Line 179: | ||
In this code: | In this code: | ||
* Firstly, we obtain the <b>type</b> information and <code>myTypeObj</code> will have the information about <code>MyClass</code> | * Firstly, we obtain the <b>type</b> information and <code>myTypeObj</code> will have the information about <code>MyClass</code> | ||
* Secondly, | |||
by using the <code>MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb"); </code>,we can get the method's information. | |||
* Thirdly, the following code will invoke the <code>AddNumb</code> method: | |||
<pre> | |||
myMethodInfo.Invoke(myClassObj, mParam); | |||
//In this example, we will use the typeof keyword to obtain the | |||
//System.Type object for a type. | |||
Public class MyClass2 | |||
{ | |||
int answer; | |||
public MyClass2() | |||
{ | |||
answer = 0; | |||
} | |||
public int AddNumb(intnumb1, intnumb2) | |||
{ | |||
answer = numb1 + numb2; | |||
return answer; | |||
} | |||
} | |||
</pre> | |||
Hence, in this way we can use reflection to obtain details of an object, method, and create objects and invoke methods at runtime. |
Revision as of 18:14, 9 October 2009
Reflection Oriented Programming
Definition
Reflection is a process of reasoning about and acting up on by itself [1]. Throwing more clarity we can presume reflection as a computer code which can be dynamically modified in the process of execution. The dynamic modification depends on the code features and its run time behavior.A system which exhibits such reflective behavior is called as reflection oriented system.
Overview
This Article throws light up on what Reflection Oriented programme is about. It deals with the need for such dynamic aspect and how it is useful in satisfying the practical needs.Further it highlights the research work that is carried out in this area and scope for future improvements.
Reflection oriented programming – Insights
Reflection
Reflection is the process of converting a programmatically expressed value into a component of interpreter state [2]. In the case of usual programs the information about itself (i.e the program) is lost as soon as it finishes compilation ,but in Reflection the program (object) needs to have information about its structures. This is done by storing it in the form of metadata[3].
There are two types of reflection based on its dynamic nature.Few programs support reflection during compilation. They are called as compile time or static reflection. Runtime reflections are those which are carried on during runtime and its the usual form of reflections.
Example of Reflections:
A simple ruby code does enough to understand the concept behind reflections
puts 12.kind_of?
The above code dynamically finds out what kind is 12 ? It returns the value integer. Thus reflection is done in ruby code to carry out operations during runtime with metaprogramming[4] enabled.
Reflection Oriented Program as Paradigm
Reflection oriented paradigm is a new paradigm used as an extension to the object oriented paradigm. It adds various features meeting the need of various applications and its very flexible. Its a dynamic run time paradigm unlike object oriented and procedure oriented model. In this paradigm objects can be varied and modified according to the needs during the run time rather than creating static set of codes with a predetermined set of operations.
The dynamic decision in this paradigm is decided based up on the data being processed and the manipulation performed on those data sets.Here is where the knowledge about its own structure plays a crucial role. On considering an example wherein a program needs to change dynamically on various sets of data (find an instance of that data or length) ,intercepting the interpreters becomes important. In these cases the program should consist of codes just to identify the data and to decide the operations to be performed on them.
Concepts
The concept of reflection oriented programming is based up on the extension of object oriented program to dynamically shape the program based on its data. All the computations can be divided in to two categories.ATOMIC and COMPOUND.Atomic refers to when the computation is completed in a single logical step whereas compound refers to the computation that requires the completion of sub computation
To make the concept work as described earlier a new entity called metainformation(information about information) is introduced.It takes into account of the details contained in the meta level.It acts as a storage wherein it contains the metainformation. It changes as the state changes and its designed to hold the metainformation of the current state.On encountering data it derives the information of the state, processes it and triggers the state change. These operations are performed during runtime
Reflectives
A reflective does the job of describing the behavior of metainformation. The current state of meta information is reflected by the instances of reflectives or objects of reflectives. Reflectives can be broadly classified in to two catgories. Simple reflective and Control reflective.Simple reflective contains the computational concepts whereas Control reflectives contains concept related to flow and control.
There is a subcategory of simple reflectives that is designed to contain the meanings of the expressed values. Its called as value reflectives. The common notation for expressing relative is <reflective>.Numerous examples of reflectives can be found here [5]
Example
Below is an example for reflection in ruby. Here the class person has a function defined which is used to display the name. An object is created to the class Person and we use obj.send to send the object to the function. This shows the dynamic behavior in ruby
class Person def hello (name) puts "hello, #{name}" end end p = Person.new p.send(:hello, "Sam")
List of Reflection Oriented Programming Languages
The list of reflection oriented programming languages includes programming languages which can support and implement reflection i.e. dynamically typed languages like Smalltalk,etc., scripting languages like PERL, etc.
The following is a comprehensive list of the reflection oriented programming languages:
- APL
- Befunge
- Curl
- Delphi
- Eiffel
- Io
- Java(java.lang.reflect)
- Lisp
- Logo
- Mathematica
- .NET Languages
- C#
- VB .NET
- Objective-C
- Perl
- PHP
- Prolog
- Python
- Ruby
- Scheme
- Smalltalk
- Snobol
- Tcl
Examples of Reflection Oriented Languages
Example in JAVA
In JAVA, reflection is implemented by using the Java package, java.lang.reflect:
/* In the following example in JAVA, identification of an array using reflection is demonstrated. */ import java.lang.reflect.*; public class FindIfArray { public static void main(String str[]){ int[] arr = {1,9,8,7}; String strg="Hello"; Class cl_arr= arr.getClass(); Class cl_strg= strg.getClass(); if(cl_arr.isArray()) System.out.println(" arr is an array "); else System.out.println(" arr is not an array "); if(cl_strg.isArray()) System.out.println(" strg is an array "); else System.out.println(" strg is not an array "); } }
The output of this above program will be as follows:
C:\Reflection\javac FindIfArray.java C:\Reflection\java FindIfArray arr is an array strg is not an array
Example in C#
To implement reflection in C#, the namespace System.Reflection is used.
The System.Reflection namespace contains classes and interfaces that provide a managed view of loaded types, methods, and fields, with the ability to dynamically create and invoke types.
using System; using System.Reflection; public class MyClass { public virtual int AddNumb(int numb1,int numb2) { int result = numb1 + numb2; return result; } } class MyMainClass { public static int Main() { Console.WriteLine ("\nReflection.MethodInfo"); // Create MyClass object MyClass myClassObj = new MyClass(); // Get the Type information. Type myTypeObj = myClassObj.GetType(); // Get Method Information. MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb"); object[] mParam = new object[] {5, 10}; // Get and display the Invoke method. Console.Write("\nFirst method - " + myTypeObj.FullName + " returns " + myMethodInfo.Invoke(myClassObj, mParam) + "\n"); return 0; } }
In this code:
- Firstly, we obtain the type information and
myTypeObj
will have the information aboutMyClass
- Secondly,
by using the MethodInfo myMethodInfo = myTypeObj.GetMethod("AddNumb");
,we can get the method's information.
- Thirdly, the following code will invoke the
AddNumb
method:
myMethodInfo.Invoke(myClassObj, mParam); //In this example, we will use the typeof keyword to obtain the //System.Type object for a type. Public class MyClass2 { int answer; public MyClass2() { answer = 0; } public int AddNumb(intnumb1, intnumb2) { answer = numb1 + numb2; return answer; } }
Hence, in this way we can use reflection to obtain details of an object, method, and create objects and invoke methods at runtime.