C# - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Reflection

C# Reflection

shape Introduction

“Reflection is the process by which a computer program can observe and modify its own structure and behavior”[http://www.adudo.com/i-know-what-you-want/]. Likewise, in C#, reflection is the ability of a managed code to interpret its own metadata for the function of finding assemblies, modules and type data/information at runtime. Hence, reflection provides objects that encapsulate assemblies, modules and cases. A program reflects on itself by extracting metadata from its assembly and using that metadata either to inform the user or to alter its own behavior. Reflections in C# are used to get the details of objects and methods and also help to create objects and invoke methods at runtime. Types can dynamically create and invoked with the help of the namespace called System.Reflection. This namespace consists of classes and interfaces that provide a managed view of loaded types, methods, and fields.

Reflection field

shape Description

Reflection field provides a means to load field values. Loop is applied through those fields and display names and values. System.Reflection, in the .NET Framework, provides a means to enumerate fields and properties.
 

Properties of Reflection

shape Properties

  1. Reflection allows to check the type of an object at runtime. (Simple calls to typeof () for instance).
  2. Reflection allows to inspect the attributes of an object at runtime to change the behavior of a method. (The various serialization methods in.NET).
  3. Reflection allows to analyze the various types in an assembly and to instantiate those types.
  4. Reflection allows for performing late binding to methods and properties and, accessing methods on types created at run time.
  5. Reflection allows to build/create new types at runtime and then does some tasks using those types.

shape Example

[csharp] using System; using System.Reflection; namespace SPLessons { class Program { public static void Main(string[] args) { ReflectionTest.Height = 10; // Set value ReflectionTest.Width = 100; // Set value ReflectionTest.Write(); // Invoke reflection methods } } static class ReflectionTest { public static int Height; public static int Width; public static void Write() { Type type = typeof(ReflectionTest); // Get type pointer FieldInfo[] fields = type.GetFields(); // Obtain all fields foreach (var field in fields) // Loop through fields { string name = field.Name; // Get string name object temp = field.GetValue(null); // Get value if (temp is int) // See if it is an integer. { int value = (int)temp; Console.Write(name + ":"); Console.WriteLine(value); } else if (temp is string) // See if it is a string. { string value = temp as string; Console.Write(name + ":"); Console.WriteLine(value); } } } } } [/csharp] Output:

shape Applications

  • Determining dependencies of an assembly.
  • Location of types which conform to an interface, derived from a base / abstract class, and searching for members by attributes
  • (Smelly) testing - If depending on a class which is untestable (ie it doesn't allow you to easily build a fake) use Reflection to inject fake values within the class--it's not pretty, and not recommended, but it can be a handy tool in a bind.
  • Debugging - dumping out a list of the loaded assemblies, their references, current methods, etc...