Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How C# Reflection Works With Code Examples

DZone's Guide to

How C# Reflection Works With Code Examples

Learn what reflection is, how it works in C#, and see some code examples of use cases where reflection allows code to inspect other code within the system.

· DevOps Zone
Free Resource

“Automated Testing: The Glue That Holds DevOps Together” to learn about the key role automated testing plays in a DevOps workflow, brought to you in partnership with Sauce Labs.

Reflection is when managed code can read its own metadata to find assemblies. Essentially, it allows code to inspect other code within the same system. To illustrate, Java’s static typing system isn’t designed to support the “doSomething” method unless the object conforms to a known interface. But with reflection, your code can view the object and find out if it has the “doSomething” method. In addition, you can call it if necessary.

With reflection in C#, you can dynamically create an instance of a type and bind that type to an existing object. Moreover, you can get the type from an existing object and access its properties. When you use attributes in your code, reflection gives you access as it provides objects of Type that describe modules, assemblies, and types.

Defining Reflection in C#

To understand reflection, there are a few basics you should understand about modules, types, and members:

  • Assemblies contain modules.
  • Modules contain types.
  • Types contain members.

You need to use Reflection when you want to inspect the contents of an assembly. For example, you can get all members of the object by typing “.” before an object when viewing your Visual Studio editor IntelliSense.

A program reflects on itself when it extracts metadata from its assemblies, then uses it to modify its own behavior or inform the user. You can compare Reflection to C++RTTI (Runtime Type Information), except that it has a much wider swath of capabilities. When you write a C# program that uses reflection, you can use either the TypeOf operator or the GetType() method to get the object’s type.

A Simple Use Case

Reflection can be used to create applications called type browsers which allow users to select types and then read the data provided about them. This example illustrates how to use the static method GetType to find the Type of a variable:

// Using GetType to obtain type information:
int i = 42;
System.Type type = i.GetType();
System.Console.WriteLine(type);

The above example results in the following output:

System.Int32

Examples of Reflection in C#

Implementing reflection in C# requires a two-step process. You first get the “type” object, then use the type to browse members such as “methods” and “properties.”

This is how you would create instances of DateTime class from the system assembly:

// create instance of class DateTime
DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));

To access the sample class Calculator from Test.dll assembly, the Calculator class should be defined as the following:

namespace Test
{
    <span class="keyword">public class</span> <span class="type">Calculator</span>
    {
        <span class="keyword">public</span> Calculator() { ... }
        <span class="keyword">private double</span> _number;
        <span class="keyword">public double</span> Number { <span class="keyword">get</span> { ... } <span class="keyword">set</span> { ... } }
        <span class="keyword">public void</span> Clear() { ... }
        <span class="keyword">private void</span> DoClear() { ... }
        <span class="keyword">public double</span> Add(<span class="keyword">double</span> number) { ... }
        <span class="keyword">public static double</span> Pi { ... }
        <span class="keyword">public static double</span> GetPi() { ... }
    }
}

Then, you can use reflection to load the Test.dll assembly:

// dynamically load assembly from file Test.dll
Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");

To create an instance of the calculator class:

// get type of class Calculator from just loaded assembly
Type calcType = testAssembly.GetType("Test.Calculator");

// create instance of class Calculator
object calcInstance = Activator.CreateInstance(calcType);

And access its members (the following examples illustrate getting values for the public double Number property):

// get info about property: public double Number
PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");

// get value of property: public double Number
double value = (double)numberPropertyInfo.GetValue(calcInstance, null);

// set value of property: public double Number
numberPropertyInfo.SetValue(calcInstance, 10.0, null);

 

How Reflection in C# Works

The main class for reflection is the System.Type class, which is an abstract class representing a type in the Common Type System (CTS). When you use this class, you can find the types used in a module and namespace and also determine if a given type is a reference or value type. You can parse the corresponding metadata tables to look through these items:

  • Fields
  • Properties
  • Fields
  • Events

Late bindings can also be achieved through reflection. To illustrate, you might not know which assembly to load during compile time. In this instance, you can ask the user to enter the assembly name and type during run time so the application can load the appropriate assembly. With the System.Reflection.Assembly type, you can get three static types which allow you to load an assembly directly:

  • LoadFrom
  • LoadFrom
  • LoadWithPartialName

When you consider that an assembly is a logical DLL or EXE and a manifest is a detailed overview of an assembly, then it makes sense that a PE (portable executable) file for CTS would have the extension of .dll or .exe. Within the PE file is mainly metadata, which contains a variety of different tables such as a:

  • Filed definition table
  • Type definition table
  • Method definition table

When you parse these tables, you can retrieve an assembly’s types and attributes.

Uses for Reflection C#

There are several uses including:

  1. Use Module to get all global and non-global methods defined in the module.
  2. Use MethodInfo to look at information such as parameters, name, return type, access modifiers and implementation details.
  3. Use EventInfo to find out the event-handler data type, the name, declaring type and custom attributes.
  4. Use ConstructorInfo to get data on the parameters, access modifiers, and implementation details of a constructor.
  5. Use Assembly to load modules listed in the assembly manifest.
  6. Use PropertyInfo to get the declaring type, reflected type, data type, name and writable status of a property or to get and set property values.
  7. Use CustomAttributeData to find out information on custom attributes or to review attributes without having to create more instances.

Other uses for Reflection include constructing symbol tables, to determine which fields to persist and through serialization.

Additional Resources and Tutorials

For more information, including some helpful tutorials, visit the following resources:

Learn about the importance of automated testing as part of a healthy DevOps practice, brought to you in partnership with Sauce Labs.

Topics:
devops ,reflection ,c#

Published at DZone with permission of Angela Stringfellow, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}