Skip to content

janluksoft/NET_ReflectionTypes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reflection in C# - dynamic manipulation of types and classes

Overview: Reflection

Reflection is an important mechanism for obtaining type information during program execution. It also allows obtaining information about an already running application, manipulating its types. It also allows for dynamic addition of types, values and objects to the application. Classes that have access to the metadata of the running program are defined in the System.Reflection namespace.

Reflection capabilities:

  • viewing attributes during program execution;
  • checking data types
  • creating type instances;
  • performing late binding to methods and properties (the target method is searched for during program execution.);
  • creating new types during the program and creating tasks with these types.

Reflection and Attribute Reading

On the subject of reflection, I have an example of a similar application here. The NET_ReflectionAttributes application uses reflection to read advanced attributes for both types and class methods.

Program operation

This program, written in .NET8 C#, demonstrates reading the types of classes and methods on the fly and, based on that, creating several instances on the fly. The class constructors and methods are run and their properties are displayed. So this is, so to speak, dynamically writing parts of the program already at runtime. This has many applications.

“Aircraft” is an example class that has several fields describing an aircraft. Each instance describes a different samolite. In the program code, an instance of 'plane' is created, from which its 3 properties are extracted, which are then set. Next, 3 constructor variables are created. Their use creates 3 new instances of InstancePlane1, 2, 3, which are initialized differently. Finally, their methods are run. In this way, full peration on the provided mechanisms of the class is achieved.

Output

The following messages are displayed as output:

An example of manipulating objects via the Reflection mechanism.

Create instance "plane" by Reflection:
Info by properties:     Aircraft name: Airbus A320, mass: 64000 kg, speed: 868,8 km/h.
Info by instance plane: Aircraft name: Airbus A320, mass: 64000 kg, speed: 868,8 km/h.

Create instances by Reflection:
Info by created plane1: Aircraft name: , mass: 0 kg, speed: 0 km/h.
Info by created plane2: Aircraft name: , mass: 7000 kg, speed: 0 km/h.
Info by created plane3: Aircraft name: Boeing B737, mass: 50000 kg, speed: 859,2 km/h.

Reflection, Info from method SetName:  Embrader
Info by instance plane: Aircraft name: Embrader, mass: 64000 kg, speed: 868,8 km/h.
Reflection, Info from method SetMass(): 7000 kg.
Info by instance plane: Aircraft name: Embrader, mass: 7000 kg, speed: 868,8 km/h.

About

.NET 8 C# app: Reflection - dynamic manipulation of types and classes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages