C# Reflection

Reflection

Reflection uses the type system. A compiled C# program is encoded into a relational database.
With reflection,
a program reads this metadata,
tests it with logic,
and makes decisions.


Reflection

Fields. We search fields with the GetField and GetFields methods. We get all the fields in a class and display their values. Next we set the values of fields through reflection.

Field ReflectionSetValue
Method

Methods. What should we do if we have the name of a method in string format and want to call the method? We can use GetMethod. Then we invoke the MethodInfo we acquire from it.

GetMethodMethodInfo Invoke
Property

Properties. A property is a kind of method. We scan properties and mutate them using the reflection mechanism.
A special type,
PropertyInfo,
must be used.

Property Reflection
About part

Types. The "Type" type is important when reflecting. We cover the GetType method that is found on the object type. GetType helps us discover the object hierarchy.

TypeGetType
Class shapes

Assembly. In reflection, an assembly contains many types. With the Assembly class, we can access these types. This helps us analyze larger units of code.

Assembly
Typeof operator

Typeof. This operator uses reflection to return the Type of a variable. We use the typeof operator in most code that requires complicated reflection capabilities.

Typeof
Size type

Sizeof. This operator exposes information about the implementation of types. It does not require the System.Reflection namespace. But it is related to reflection.

Sizeof
Default operator

Default. This operator returns the default value for a type. It is used when developing classes such as generics. The default value of an int is zero.

Default
Question

Research. When a C# program is compiled, it is actually converted into a relational database. When the runtime starts, it reads this program data. It constructs further representations.


Expert .NET 2.0 IL Assembler

From .NET IL Assembler. "Structurally, metadata is a normalized relational database. This means that metadata is organized as a set of cross-referencing rectangular tables" (page 73).


Programs that use reflection can look inside themselves to see their inner workings. This capacity lends them unique and powerful features. But it also makes them slower, more complex.


C#: .NET