Reflection of types. Getting type metadata. The System.Reflection namespace. Class System.Type. Ways to get information about type. Operator typeof
Contents
- 1. What tools of Microsoft Visual Studio is possible to use for analyze basic information about the assembly?
- 2. What types can be fully described using metadata?
- 3. What is the reflection in .NET?
- 4. What are the advantages of using reflection in programs?
- 5. In which assembly and in which namespace the reflection is implemented in C# .NET?
- 6. List and assignment of the most important types from the System.Reflection namespace
- 7. What is the main purpose of the System.Type class? How the System.Type class is related with a reflection?
- 8. How to get information about the type in the program? What are the ways to programmatically obtain information about the type?
- 9. How can I get type information using the System.Object.GetType() method? Example
- 10. How is possible to get the type information using the typeof() tool? Example
- 11. How can I get type information using System.Type.GetType()? Examples
- Related topics
Search other websites:
1. What tools of Microsoft Visual Studio is possible to use for analyze basic information about the assembly?
Microsoft Visual Studio includes special utilities ildasm.exe and reflector.exe. These utilities allow you to analyze:
- CIL-code of assembly;
- metadata of types that are implemented in the assembly;
- manifest of assembly for any binary .NET file.
In addition, in Microsoft Visual Studio, the same information can be obtained programmatically using the System.Reflection namespace.
⇑
2. What types can be fully described using metadata?
Using metadata, you can fully describe the following types:
- classes (class);
- interfaces (interface);
- structures (struct);
- enumerations (enum);
- delegates (delegate).
⇑
3. What is the reflection in .NET?
Reflection (reflection) in .NET is the process of identifying metadata (types) during program execution as an object model. In other words, reflection is a means to get information about a data type. Reflection allows you to get exactly the same information about the metadata types that can be obtained using the utility ildasm.exe (IL Disassembler).
Reflection allows you to get a list of all types that are placed inside:
- the executable module *.exe;
- the assembly *.dll;
- files with *.mod and *.mdl extensions in the case of a multi-file assembly.
⇑
4. What are the advantages of using reflection in programs?
With the help of reflection tools (System.Type class) you can get information about types and their characteristics in the assembly during program execution. This provides the following benefits:
- it is possible create types and call their methods without previous knowledge of the names that are used in these types. This implements the so-called dynamic type identification;
- at the compilation time it is not necessary to know the information about the type from which the metadata will be pulled. It is enough to know only the name of the type. The type name is specified in the String() string, which is publicly available anywhere in the program;
- representation of types, that are pulled out of the assembly, as the form of a convenient object model.
⇑
5. In which assembly and in which namespace the reflection is implemented in C# .NET?
In C# .NET, reflection is implemented in the System.Reflection namespace, which is supplied as part of the mscorlib.dll assembly.
To use reflection, you need to include the namespace System.Reflection in your program
using System.Reflection;
⇑
6. List and assignment of the most important types from the System.Reflection namespace
The System.Reflection namespace has many types for implementing reflection. However, the most important are the following:
- Assembly – abstract class. It contains static methods for working with the assembly. These methods allow, for example, to load the assembly;
- AssemblyName – this is a class that contains information that is used to identify the assembly. For example: the version number of the assembly, information about the culture, etc.;
- EventInfo – abstract class. Contains information about a specified event;
- FieldInfo – abstract class. It can contain information about the specified data members of the class;
- MemberInfo – abstract class. Contains general behavior information for classes (types) EventInfo, FieldInfo, MethodInfo and PropertyInfo;
- MethodInfo – abstract class. Contains information about specified method;
- Module – abstract class. It allows you to get information about a given module in the case of a multi-file assembly;
- ParameterInfo – a class that contains information about a given parameter in a given method;
- PropertyInfo – abstract class. It contains information about the specified property.
To get information about a given class, the methods of the System.Type class are used, which return the result in the form of arrays or individual classes of the above types (Assembly, MethodInfo, ParameterInfo, etc.).
⇑
7. What is the main purpose of the System.Type class? How the System.Type class is related with a reflection?
The System.Type class is useful when you need to study the metadata of some type (class, interface, structure, enumeration, delegate). The System.Type class encapsulates a data type. The methods of the System.Type class return types from the System.Reflection namespace.
The System.Type class serves as the basis of reflection. Using the methods of the System.Type class, you can determine information about the types used during program execution. Then this information can be analyzed, processed, depending on the task.
⇑
8. How to get information about the type in the program? What are the ways to programmatically obtain information about the type?
In C# .NET, there are three ways to programmatically retrieve type information:
- Using the System.Object.GetType() method. In this case, method returns the metadata of the current object of type (see p. 9).
- Using the typeof() tool. In this case, create the object of type (for example, an object of a class) is not necessary. It is sufficient to have a declared type (see p. 10).
- Using the static method System.Type.GetType().This approach does not need to declare the type whose information is to be obtained (see p. 11). It is enough to know only the name of this type.
⇑
9. How can I get type information using the System.Object.GetType() method? Example
The type can be a class, interface, structure, enumeration, delegate. This method needs to obtain information about the type under investigation at the compilation stage. That is, the type declaration must be known before compiling.
For example, to use this method for a class you need:
- create a class object using new operator;
- call the class method obj.GetType(), where obj is the name of the object of this class.
Previously you need to include the System.Reflection namespace in your project.
using System.Reflection;
Example. An MathFunctions class declaration is defined that contains 3 methods and 3 data members:
- method Min2() – returns a minimum between two values;
- method Min3() – returns a minimum between three values;
- method Max2() – returns a maximum between three values;
- internal data members of the class with the names a, b, c.
class MathFunctions { public int a, b, c; // data members of class // minimum between two values public int Min2(int a, int b) { if (a < b) return a; return b; } // minimum between three values public int Min3(int a, int b, int c) { int min = a; if (min > b) min = b; if (min > c) min = c; return min; } // maximum between two values public int Max2(int a, int b) { if (a < b) return b; return a; } }
You can get the list of methods of the MathFunctions class programmatically as follows.
class Program { static void Main(string[] args) { // get the value of the type MathFunctions mf = new MathFunctions(); Type tp = mf.GetType(); // get a list of methods from the MathFunctions class MethodInfo[] mi = tp.GetMethods(); // get method names - simplified version string s1 = mi[0].Name; // s1 = "Min2" string s2 = mi[1].Name; // s2 = "Min3" string s3 = mi[2].Name; // s3 = "Max2" Console.WriteLine("Method1 = {0}", s1); // Method1 = Min2 Console.WriteLine("Method2 = {0}", s2); // Method2 = Min3 Console.WriteLine("Method3 = {0}", s3); // Method3 = Max2 } }
In this example, the MathFunctions class can be implemented
- in the same namespace;
- in another assembly. In this case, the assembly with the declared class can be connected in the References section as well as with the using directive.
⇑
10. How is possible to get the type information using the typeof() tool? Example
The type can be a class, interface, structure, enumeration, delegate. To get information about the type using the typeof() tool, as well as in the preceding paragraph, you need to have information about the type at the compile time.
For example, to use this method for a class, you do not need to first create a class object. In general, a string that receives information about a certain class has the following form:
Type tp = typeof(ClassName);
where ClassName – the name of some class for which an object is created with the name tp containing information about this class.
Previously, the System.Reflection namespace must be included at the beginning of the program module:
using System.Reflection;
Example. Let the class MathFunctions be given, declared in Section 9. The class contains three methods with the names Min2(), Min3(), Max2() and three internal variables with the names a, b, c.
Then, obtaining information about methods and internal class variables using the typeof() tool can be as follows:
... class Program { static void Main(string[] args) { // get the value of the type Type tp = typeof(MathFunctions); // get a list of methods from the MathFunctions class MethodInfo[] mi = tp.GetMethods(); // get method names string m1 = mi[0].Name; // m1 = "Min2" string m2 = mi[1].Name; // m2 = "Min3" string m3 = mi[2].Name; // m3 = "Max2" // get an internal fields of class FieldInfo[] fi = tp.GetFields(); string f1 = fi[0].Name; // the name of internal variable a, f1 = "a" string f2 = fi[1].Name; // f2 = "b" string f3 = fi[2].Name; // f3 = "c" Console.WriteLine("Method1 = {0}", m1); // Method1 = Min2 Console.WriteLine("Method2 = {0}", m2); // Method2 = Min3 Console.WriteLine("Method3 = {0}", m3); // Method3 = Max2 Console.WriteLine("Field1 = {0}", f1); // Field1 = a Console.WriteLine("Field2 = {0}", f2); // Field2 = b Console.WriteLine("Field3 = {0}", f3); // Field3 = c } } ...
Important: in order to obtain information about internal variables and methods of a class, these variables and methods must be publicly accessible, that is, declared with the public access modifier. Otherwise, hidden internal variables and hidden methods are invisible.
⇑
11. How can I get type information using System.Type.GetType()? Examples
The type can be a class, interface, structure, enumeration, delegate. Information about the type (class, interface, structure, etc.) is obtained using the static GetType() method of the System.Type class. The GetType() method receives string of type System.String as input parameter. This string should contain the name of the assembly and the name of the type (class, interface, …) about which you need to determine information.
Example 1. An assembly named MyAssembly is specified. A class, named MyClass is declared in the assembly. Then the string that returns the class that will implement the assembly is as follows
... System.Type T = GetType("MyAssembly.MyClass"); ...
After that, the instance (object) T will contain information about the class MyClass.
Example 2. The use of System.Type.GetType() to access the MathFunctions class is demonstrated (see p. 9). In this example, it is assumed that the MathFunctions class is declared within the namespace TrainReflections1. The program code for the entire module, which demonstrates the use of the class for the console application is the following:
using System; using System.Collections.Generic; using System.Linq; using System.Text; // connect the System.Reflection namespace using System.Reflection; namespace TrainReflection1 { class MathFunctions { public int a, b, c; // data members of class // minimum between two values public int Min2(int a, int b) { if (a < b) return a; return b; } // minimum between three values public int Min3(int a, int b, int c) { int min = a; if (min > b) min = b; if (min > c) min = c; return min; } // maximum between two values public int Max2(int a, int b) { if (a < b) return b; return a; } } class Program { static void Main(string[] args) { // get the value of the type Type tp = null; tp = Type.GetType("TrainReflection1.MathFunctions"); // get a list of methods from the MathFunctions class MethodInfo[] mi = tp.GetMethods(); // get the names of methods string m1 = mi[0].Name; // m1 = "Min2" string m2 = mi[1].Name; // m2 = "Min3" string m3 = mi[2].Name; // m3 = "Max2" // get a list of internal class data FieldInfo[] fi = tp.GetFields(); string f1 = fi[0].Name; // f1 = "a" string f2 = fi[1].Name; // f2 = "b" string f3 = fi[2].Name; // f3 = "c" Console.WriteLine("Method1 = {0}", m1); // Method1 = Min2 Console.WriteLine("Method2 = {0}", m2); // Method2 = Min3 Console.WriteLine("Method3 = {0}", m3); // Method3 = Max2 Console.WriteLine("Field1 = {0}", f1); // Field1 = a Console.WriteLine("Field2 = {0}", f2); // Field2 = b Console.WriteLine("Field3 = {0}", f3); // Field3 = c } } }
⇑
Related topics
- Run-time type information (RTTI). Statements is, as. Examples
- Examples of obtaining information about methods, interfaces, classes, structures, enumerations, delegates, type fields, method parameters, type statistics
- Dynamically loadable assemblies. Class Assembly. Methods Load() and LoadFrom()