Because the identify suggests, the reflection is one thing that provides an info picture about one thing. Inside C#, a mirrored image is a well known namespace discovered within the “System” library to see metadata in regards to the courses, strategies, parameters, or sorts. It comprises lots of courses and attributes to get completely different info relating to the code elements. So, we’ve determined to carry out some quite simple examples to elaborate on using reflection namespace together with its attributes. Let’s get began with the C# file creation.

Instance # 01:

Getting began with the primary instance, we can be wanting on the elaboration of using the “System.Reflection” class to search out metadata info relating to differing types, strategies, or fields. So, we’ve began this code with using the System library, and after that, we’ve utilized the “System.Reflection” class within the code. We’ve began a namespace Take a look at and a category Take a look at inside it.

This class Take a look at comprises a single perform, “Important()” which goes to be our driver perform for this code. The primary() perform has been began with the key phrase “Kind,” loading a sort “t1” of a string through the “typeof()” perform from the System class. The “Kind” got here up from the Reflection class. Now, the identical process has been used to load the kind of t2 and t3 of an integer and character, respectively. As we all know that the Reflection class got here up with most of the metadata attributes to search out out the knowledge relating to sorts, strategies, and fields.

So, we’ll use these metadata attributes right here to search out info relating to sorts t1, t2, and t3. Thus, we’ve used three Console.WriteLine() perform statements to show the Title, FullName, Namespace and BaseType of sort t1, t2, and t3 individually. For this, we’ve been using the “Title,” “FullName,” “Namespace,” and “BaseType” attributes of a mirrored image class right here within the code.

We’ve been compiling this newly made “reflection.cs” file with the C# compiler “mcs” already built-in in our Linux system. The “exe” file for this code has been generated, and we’ve used it to run the compiled code with the “mono” runtime command. The output displayed on our display screen exhibits us the Title, FullName, Namespace, and BaseType info for the loaded sort string, integer, and character.

Instance # 02:

In our first instance, we obtained info relating to Kind in C# metadata utilizing the Reflection class attributes. We can be utilizing the Reflection class to get metadata info relating to the loaded “Meeting.” So, we’ve been beginning our instance with using the “System” library within the first line and utilizing the “System.Reflection” class within the second line.

This code comprises a namespace Take a look at with the identical identify class Take a look at. On this class Take a look at, we’ve been utilizing the Important() driver perform to begin executing our system. Inside the principle() perform, execution begins from loading an meeting “t.” This loaded meeting variable “t” can be getting the metadata info relating to the “int” meeting of C# through the “typeof” perform utilizing “int” inside it, adopted by the dot and “Meeting” key phrase. The following precise line has the Console.WriteLine() perform to show the metadata info relating to the meeting “t.” The category and namespace are accomplished right here, and the code is saved to be used.

We’ve compiled this newly made C# code with the “mcs” compiler after which executed it with the “mono” runtime executor for C# in Ubuntu 20.04. It displayed the identify of a library for “t” meeting as “mscorlib,” its model, tradition, and public key token on our display screen beneath.

Instance # 03:

Inside our final instance, we can be looking on the metadata info for all of the courses, strategies, attributes, or parameters used within the code through the Reflection class. So, we’ve been beginning this code with the identical code sample as we’ve used within the above two examples, i.e., using the System library and the “System.Reflection” class. A namespace “New” has been created that holds two courses in it, i.e., Data and Take a look at.

Let’s focus on the “Data” class first. Inside this class, we’ve been utilizing defining the properties or variables we’ve to make use of on this code, i.e., Age of int sort and Title of string sort. The “get” and “set” attributes are utilized to get the worth for each values and assign them to the variables Age and Title, respectively. Listed below are two constructor features within the class Data. The primary constructor has no parameters, whereas the opposite is a parameterized constructor getting values in new variables “a” and “n.” The easy constructor initializes the values of variable Age and Title to 25 and “Empty,” respectively.

Then again, the opposite constructor has been assigning random variable values to Age and Title variables, i.e., empty. The user-defined “present()” perform has been used right here to show the variable ‘Age’ and ‘Title’ values on the Console of Ubuntu 20.04 utilizing the WriteLine() perform from the Console class of the System library. The primary class has been accomplished right here.

Let’s focus on the “Take a look at” class the place we’ve been using the “Reflection” attributes to get metadata info relating to the code. So, it comprises a important() technique that’s our executor for this code. We’ve loaded an object for sophistication meeting “Asb” to get the at present executing meeting with the ‘GetExecutingAssembly’ perform. This object “Asb” has been used to name the “GetTypes()” perform to retailer the meeting sort info throughout the Kind Array “t.”

Now, it’s time to iterate every sort from the meeting and get info relating to its metadata. So, throughout the first “foreach” loop, we’ve been iterating the sort used within the code through the Kind array “t,” i.e., courses. The identify of the whole courses used can be displayed by the “Title” attribute of the Reflection class. An array “Met” of MethodInfo sort has been initialized to get complete strategies within the particular class utilizing the iterator “I.” An interior “foreach” loop is used to show the identify of every technique throughout the particular class through the Title attribute.

The ParameterInfo sort array “Par” is outlined to get the parameter info for all of the strategies within the particular class utilizing the “GetParameters()” perform. The final interior loop is used to iterate all of the parameters “p” discovered within the “Par” array to show the Parameter identify and ParameterType on the shell utilizing the Title and ParameterType attribute of the Reflection class. Our code is now full and prepared for compilation.

The metadata info relating to “Data” and “Take a look at” courses is displayed individually. The courses have parameter int sort strategies; string sort strategies; and Object sort strategies

Conclusion:

That is how a Reflection namespace class will be utilized for getting metadata info relating to strategies, sorts, parameters, assemblies, and courses. Inside our first two examples, we’ve mentioned using Title, FullName, Namespace, BaseType attribute of the reflection class to get info relating to the loaded sorts and meeting, i.e., int, char, string. Within the final instance, we’ve used the GetTypes, GetMethods(), GetParameters, and the identify and Parameter sort attribute of the Reflection class to get metadata about the entire code.

Did you want this text?
Share it on any of the next social media channels beneath to offer us your vote. Your suggestions helps us enhance.

close