DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • The Features of C# 9 That Will Make Your Life Easier [Snippets]
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server
  • Using AUTHID Parameter in Oracle PL/SQL
  • How To Generate Scripts of Database Objects in SQL Server

Trending

  • Exploring Data Redaction Enhancements in Oracle Database 23ai
  • The Scrum Guide Expansion Pack
  • A Complete Guide to Modern AI Developer Tools
  • Run Scalable Python Workloads With Modal
  1. DZone
  2. Data Engineering
  3. Databases
  4. How C# Reflection Works With Code Examples

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.

By 
Angela Stringfellow user avatar
Angela Stringfellow
·
Aug. 11, 17 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
14.6K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • TutorialsPoint – C# Reflection
  • CodeProject – Reflection in C# Tutorial
  • CSharp.net – Reflection Introduction
  • .NET Academy – Introduction to Reflection API
  • Dot Net Perls – Reflection
csharp Database Assembly (CLI) Object (computer science)

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

Opinions expressed by DZone contributors are their own.

Related

  • The Features of C# 9 That Will Make Your Life Easier [Snippets]
  • Monitoring and Managing the Growth of the MSDB System Database in SQL Server
  • Using AUTHID Parameter in Oracle PL/SQL
  • How To Generate Scripts of Database Objects in SQL Server

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: