top of page

5_RA

  • hrafnulf13
  • Oct 23, 2020
  • 3 min read

Updated: Oct 28, 2020


Reflection. Reflection objects are used for obtaining type information at runtime [1]. The classes that give access to the metadata of a running program are in the System.Reflection namespace.The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.


Reflection has the following applications:

  • It allows view attribute information at runtime.

  • It allows examining various types in an assembly and instantiate these types.

  • It allows late binding to methods and properties

  • It allows creating new types at runtime and then performs some tasks using those types.


Type "Type". The System.Type class is the main class for the .NET Reflection functionality to access metadata [4, 5]. Type class represents class types, interface types, array types, value types, enum types, type parameters, generic type definitions, and open/closed generic types. Type class helps to find properties, methods, events, fields, and constructors declared in a type. It also provides us the Assembly in which the type is declared.

There are several ways to get the instance of Type class.

  1. typeof(ClassName)

  2. obj.GetType(), is an instance method that can only be used on the object of that particular Type.

  3. Type.GetType()


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;
  }
}

A more detailed information about Reflection and examples can be viewed in [1-7]. The reason for this research was to find a way to dinamically change the types of the variable during the runtime of 7_A application. I did it my own way, so there was no need for me to use Reflection in my application. I didn't use Reflection, since upon reading the task I already had an idea on how to store and get info about the values.

I created 2 classes: Variable and Value, and one enum type Variable_Type (Integer, Double, Boolean, String, DateTime)


Variable stores the general information of the values, such as:

  • m_name of the variable

  • m_default_type of the variable, that was defined during reading of the file

  • m_type, which is set to default type from the beginning but can be modified by user, so that all values of the variable will be changed upon changing it.

  • List of Values (objects)

  • So Variable class can be considered as a column with values

Value stores the general information of value itself, such as:

  • m_generic_value, which is initial value that was read from the file (used for restoring values, for example when casting from double to int and back).

  • m_value of the dynamic type. After reading the file is set to m_generic_value and converted to type specified by the m_type. So the type of the m_value changes when user changes m_type value during runtime.

  • m_default_type of the variable, that was defined during reading of the file and usually retrieved from the Variable object that stores that Value.

  • m_type, which is set to default type from the beginning but can be modified by user, so that all values of the variable will be changed upon changing it. Usually retrieved from the Variable object that stores that Value.

The concept might look identical, where I store the type of the variable and change it during runtime and store in dynamic type value.




 

References


Recent Posts

See All
14_RA

The Euler–Maruyama method (also called the Euler method) is a method for the approximate numerical solution of a stochastic differential...

 
 
 

Commentaires


bottom of page