- HTML Comments Tutorial | Convert Comments into HTML Codes
- Data Structure and Algorithms Tutorial | Ultimate Guide to Learn
- Gradle Tutorial | For Beginners [ STEP-IN ]
- Encapsulation In Java | Complete Tutorial With Examples
- What is Release Management? | A Comprehensive Tutorial for Beginners
- OOPs Concepts in Java | Learn from Basics with Examples
- The Top Basic Tools of Quality Tutorial | The Ultimate Guide
- Set Environment for C# – Learn How to Setup through this Tutorial
- C# Vs Java Tutorial | Overview and Key Difference
- List of IDEs to run C# Programs | Tutorial for Learning Path
- C Sharp Variables and Constants | The Ultimate Guide
- Unsafe Code in C-Sharp Tutorial | Everything You Need to Know
- Type Conversion Method in C# | A Complete Tutorial
- What Is Synchronization in c# Tutorial | The BEST Step-By-Step Guide
- Understanding Structures in C# | Learn in 1 Day FREE Tutorial
- Strings – C# Tutorial | A Definitive Programming Guide
- Static Keyword in C# Tutorial | Learn with Examples
- Stack Collection in C# Tutorial | A Definitive Guide for Beginners
- C# Sorted List Tutorial with Examples | Learn in 1 Day FREE
- C# Serialization Tutorial | Ultimate Guide to Learn [BEST & NEW]
- Regular Expression in C# Tutorial | Everything You Need to Know
- What is Reflection in C#? | Learn Now Tutorial
- Queue Collection in C# Tutorial | A Definitive Guide
- Properties in C# | The complete Tutorial
- C# Preprocessor Directives Tutorial | Learn in 1 Day FREE
- Polymorphism C# Tutorial | The Ultimate Guide
- C# Operators Tutorial | Learn Arithmetic, Comparison, Logical Concepts
- Namespaces – The complete C# Tutorial
- Multithreading in C# Tutorial | Learn With Examples and Advantages
- Methods – C# Tutorial | A Complete Programming Guide
- Linked List Implementation in C# Tutorial | Ultimate Guide to Learn [UPDATED]
- Introduction to C# Tutorial | Guide for Beginners
- What is Interface in C# | A Defined Free Tutorial
- C# Inheritance Tutorial | A Complete Free Tutorial
- Indexers in C# Tutorial | A Complete Programming Guide
- HashSet Collection in C# Tutorial | Complete Guide Tutorial For Free
- Generics in C# Tutorial | Learn Generic Classes and Methods
- Creating Your First C# Program Tutorial | Learn in 1 Day
- Basics of File Handling in C# Tutorial | The Ultimate Guide
- C# Exception Handling Tutorial | Learn with Best Practices
- Events – C# Tutorial | A Complete Programming Guide
- C# Enumerations Type Tutorial | Learn Everything about Enum
- Dictionary Collection in C# | Ultimate Guide to Learn [NEW & UPDATED]
- Delegates – C# Programming Guide | The Ultimate Guide for Beginners
- Understanding Decision Making Statements in C# | Learn Now Tutorial
- Classes and Objects – C# Fundamentals Tutorial
- C# BitArray Collection Tutorial | Learn in 1 Day FREE
- Attributes in C# Tutorial | Learn to work with attributes in C#
- C# Array Tutorial | Create, Declare, Initialize
- ArrayList Collection on in C# | A Complete tutorial For Beginners
- Anonymous Methods and Lambdas – C# Tutorial | A Complete Guide
- Abstraction in C# Tutorial – Learn the Abstract class and Interface
- Game Development using Unity 3D Tutorial | Ultimate Guide to Learn [UPDATED]
- C++ Reference Tutorial | A Comprehensive Guide for Beginners
- PHP vs Python | Which Is Better For Web Development
- C++ Constructors Tutorial: Types and Copy Constructors
- JavaScript Arrays Tutorial | Complete Beginner’s Guide
- What Is Maven | Maven Tutorial For Beginners
- Spring Tutorial | Perfect Guide for Beginners
- React Hooks Tutorial for Beginners | Ultimate Guide to Learn
- Python for Data Science Tutorial | Quickstart : A Complete Guide
- What is Golang? : A tutorial for beginners | Get started
- Hibernate Validator Tutorial | Learn in 1 Day FREE
- Postman Tutorial for Beginners: API Testing using Postman | A Complete Guide
- Akka Tutorial
- J2EE | Web Development Tutorial for Beginners
- Scala Exception Handling Tutorial | Learn in 1 Day [ STEP-IN ]
- Web development Tutorial
- Visual Studio Tutorial
- PyGame Tutorial
- Python Anaconda Tutorial
- Python Scikit-Learn Cheat Sheet Tutorial
- Mean Stack Tutorial
- Python Requests Tutorial
- Advanced Java Tutorial
- Spring Boot Microservices Tutorial
- Java Servlets Tutorial
- How to Install Pycharm
- Pycharm Tutorial
- Python Version Tutorial
- Python strings
- How to Download Python
- C Data Types Tutorial
- arrays in python
- Python While Loop Tutorial
- JAVA Tutorial
- Loops In C Tutorial
- Java File I/O Tutorial
- Variables in Python Tutorial
- Python Tutorial
- Python Pandas Cheat Sheet Tutorial
- Data Structures Cheat Sheet with Python Tutorial
- Python Tuples Tutorial
- Python If Else Statements Tutorial
- Python Functions Tutorial
- HTML Comments Tutorial | Convert Comments into HTML Codes
- Data Structure and Algorithms Tutorial | Ultimate Guide to Learn
- Gradle Tutorial | For Beginners [ STEP-IN ]
- Encapsulation In Java | Complete Tutorial With Examples
- What is Release Management? | A Comprehensive Tutorial for Beginners
- OOPs Concepts in Java | Learn from Basics with Examples
- The Top Basic Tools of Quality Tutorial | The Ultimate Guide
- Set Environment for C# – Learn How to Setup through this Tutorial
- C# Vs Java Tutorial | Overview and Key Difference
- List of IDEs to run C# Programs | Tutorial for Learning Path
- C Sharp Variables and Constants | The Ultimate Guide
- Unsafe Code in C-Sharp Tutorial | Everything You Need to Know
- Type Conversion Method in C# | A Complete Tutorial
- What Is Synchronization in c# Tutorial | The BEST Step-By-Step Guide
- Understanding Structures in C# | Learn in 1 Day FREE Tutorial
- Strings – C# Tutorial | A Definitive Programming Guide
- Static Keyword in C# Tutorial | Learn with Examples
- Stack Collection in C# Tutorial | A Definitive Guide for Beginners
- C# Sorted List Tutorial with Examples | Learn in 1 Day FREE
- C# Serialization Tutorial | Ultimate Guide to Learn [BEST & NEW]
- Regular Expression in C# Tutorial | Everything You Need to Know
- What is Reflection in C#? | Learn Now Tutorial
- Queue Collection in C# Tutorial | A Definitive Guide
- Properties in C# | The complete Tutorial
- C# Preprocessor Directives Tutorial | Learn in 1 Day FREE
- Polymorphism C# Tutorial | The Ultimate Guide
- C# Operators Tutorial | Learn Arithmetic, Comparison, Logical Concepts
- Namespaces – The complete C# Tutorial
- Multithreading in C# Tutorial | Learn With Examples and Advantages
- Methods – C# Tutorial | A Complete Programming Guide
- Linked List Implementation in C# Tutorial | Ultimate Guide to Learn [UPDATED]
- Introduction to C# Tutorial | Guide for Beginners
- What is Interface in C# | A Defined Free Tutorial
- C# Inheritance Tutorial | A Complete Free Tutorial
- Indexers in C# Tutorial | A Complete Programming Guide
- HashSet Collection in C# Tutorial | Complete Guide Tutorial For Free
- Generics in C# Tutorial | Learn Generic Classes and Methods
- Creating Your First C# Program Tutorial | Learn in 1 Day
- Basics of File Handling in C# Tutorial | The Ultimate Guide
- C# Exception Handling Tutorial | Learn with Best Practices
- Events – C# Tutorial | A Complete Programming Guide
- C# Enumerations Type Tutorial | Learn Everything about Enum
- Dictionary Collection in C# | Ultimate Guide to Learn [NEW & UPDATED]
- Delegates – C# Programming Guide | The Ultimate Guide for Beginners
- Understanding Decision Making Statements in C# | Learn Now Tutorial
- Classes and Objects – C# Fundamentals Tutorial
- C# BitArray Collection Tutorial | Learn in 1 Day FREE
- Attributes in C# Tutorial | Learn to work with attributes in C#
- C# Array Tutorial | Create, Declare, Initialize
- ArrayList Collection on in C# | A Complete tutorial For Beginners
- Anonymous Methods and Lambdas – C# Tutorial | A Complete Guide
- Abstraction in C# Tutorial – Learn the Abstract class and Interface
- Game Development using Unity 3D Tutorial | Ultimate Guide to Learn [UPDATED]
- C++ Reference Tutorial | A Comprehensive Guide for Beginners
- PHP vs Python | Which Is Better For Web Development
- C++ Constructors Tutorial: Types and Copy Constructors
- JavaScript Arrays Tutorial | Complete Beginner’s Guide
- What Is Maven | Maven Tutorial For Beginners
- Spring Tutorial | Perfect Guide for Beginners
- React Hooks Tutorial for Beginners | Ultimate Guide to Learn
- Python for Data Science Tutorial | Quickstart : A Complete Guide
- What is Golang? : A tutorial for beginners | Get started
- Hibernate Validator Tutorial | Learn in 1 Day FREE
- Postman Tutorial for Beginners: API Testing using Postman | A Complete Guide
- Akka Tutorial
- J2EE | Web Development Tutorial for Beginners
- Scala Exception Handling Tutorial | Learn in 1 Day [ STEP-IN ]
- Web development Tutorial
- Visual Studio Tutorial
- PyGame Tutorial
- Python Anaconda Tutorial
- Python Scikit-Learn Cheat Sheet Tutorial
- Mean Stack Tutorial
- Python Requests Tutorial
- Advanced Java Tutorial
- Spring Boot Microservices Tutorial
- Java Servlets Tutorial
- How to Install Pycharm
- Pycharm Tutorial
- Python Version Tutorial
- Python strings
- How to Download Python
- C Data Types Tutorial
- arrays in python
- Python While Loop Tutorial
- JAVA Tutorial
- Loops In C Tutorial
- Java File I/O Tutorial
- Variables in Python Tutorial
- Python Tutorial
- Python Pandas Cheat Sheet Tutorial
- Data Structures Cheat Sheet with Python Tutorial
- Python Tuples Tutorial
- Python If Else Statements Tutorial
- Python Functions Tutorial

What is Reflection in C#? | Learn Now Tutorial
Last updated on 16th Aug 2022, Blog, Software Engineering, Tutorials
C# – Reflection example
Reflection objects are used for obtaining type information at the runtime. The classes that give access to the metadata of a running program are in the System.Reflection namespace. The System.Reflection namespace contains classes that permit to obtain information about the application and to dynamically add types, values, and objects to the application.
What is Programming Reflection?
Reflection can offer information about the assemblies that have been loaded and metadata about types declared in them. The metadata information on a type is made accessible by the Type abstract class Reflection in C# is like native RTTI (Runtime Type Information) of C++, there is a significant difference: reflection in C# works only with maintaining code and is more powerful. The reflection types in .NET are present in a System.Reflection namespace. To work with reflection in C#, must include the System.Reflection namespace in the program.
Defining Reflection in C#
To understand reflection, there are a basics should understand about modules, types, and members:

- Assemblies contain modules
- Modules contain types
Need to use Reflection when wanting to inspect the contents of an assembly.
For example, you can get all members of an object by typing “.” before an object when viewing the Visual Studio editor IntelliSense. A program reflects on itself when it extracts metadata from its assemblies, then uses it to change its own behavior or inform the user Compare Reflection to C++RTTI (Runtime Type Information), except that it has a lot of capabilities. When write a C# a program that uses reflection, can use either the TypeOf operator or the GetType() method to get the object’s type.
C# Reflection
In C#reflection is a process to get metadata of the type at runtime. The System.Reflection namespace contains required classes for reflection such as:
- Type
- MemberInfo
- ConstructorInfo
- MethodInfo
- FieldInfo
- PropertyInfo
- TypeInfo
- EventInfo
- Module
- Assembly
- AssemblyName
- Pointer etc
The System.Reflection.Emit namespace contains classes to emit the metadata.
A Simple Use Case
Reflection can be used to generate applications called type browsers which allow users to select types and then read the data provided about them.
C# Type class
C# Type class denote type declarations for class types, interface types, enumeration types, array types, value types etc
- It is find in System namespace
- It inherits the System.Reflection.MemberInfo class.
C# Reflection Example: Get Type
- using System;
- public class ReflectionExample
- {
- public static void Main()
- {
- int a = 10;
- Type type = a.GetType();
- Console.WriteLine(type);
- }
- }
- C# Reflection Example: Get Assembly
- using System;
- using System.Reflection;
- public class ReflectionExample
- {
- public static void Main()
- {
- Type t = typeof(System.String);
- Console.WriteLine(t.Assembly);
- }
- }
C# Reflection Example: Get Assembly
- using System;
- using System.Reflection;
- public class ReflectionExample
- {
- public static void Main()
- {
- Type t = typeof(System.String);
- Console.WriteLine(t.Assembly);
- }
- }
C# Reflection Example: Print Type Information
- using System;
- using System.Reflection;
- public class ReflectionExample
- {
- public static void Main()
- {
- Type t = typeof(System.String);
- Console.WriteLine(t.FullName);
- Console.WriteLine(t.BaseType);
- Console.WriteLine(t.IsClass);
- Console.WriteLine(t.IsEnum);
- Console.WriteLine(t.IsInterface);
- }
- }
C# Reflection Example: Print Constructors
- using System;
- using System.Reflection;
- public class ReflectionExample
- {
- public static void Main()
- {
- Type t = typeof(System.String);
- Console.WriteLine(“Constructors of {0} type…”, t);
- ConstructorInfo[] ci = t.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
- foreach (ConstructorInfo c in ci)
- {
- Console.WriteLine(c);
- }
- }
- }
C# Reflection Example: Print Methods
- using System;
- using System.Reflection;
- public class ReflectionExample
- {
- public static void Main()
- {
- Type t = typeof(System.String);
- Console.WriteLine(“Methods of {0} type…”, t);
- MethodInfo[] ci = t.GetMethods(BindingFlags.Public | BindingFlags.Instance);
- foreach (MethodInfo m in ci)
- {
- Console.WriteLine(m);
- }
- }
- }
C# Reflection Example: Print Fields
- using System;
- using System.Reflection;
- public class ReflectionExample
- {
- public static void Main()
- {
- Type t = typeof(System.String);
- Console.WriteLine(“Fields of {0} type…”, t);
- FieldInfo[] ci = t.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic);
- foreach (FieldInfo f in ci)
- {
- Console.WriteLine(f);
- }
- }
- }
How Reflection in C# Works:

The major class for reflection is the System.Type class, which is an abstract class representing a type in the Common Type System (CTS). When using this class, can find the types used in a module and namespace and also find if a given type is a reference or value type. The corresponding metadata tables to look through these items:
- Fields
- Properties
- Methods
- Events
Late bindings can also be achieved through reflection.Might not know which arrangement to load during compile time. With the System.Reflection.Assembly type can get 3 static types which allow to load an assembly directly:
- LoadFrom
- LoadFrom
- LoadWithPartialName
When considering that an assembly is a logical DLL or EXE and a manifest is an explained overview of an assembly, then it makes sense that a PE (portable executable) file for CTS has the extension of .dll or .exe. Within the PE file is mainly metadata, which contains a different tables such as a:
- Filed definition table
- Type definition table
- Method definition table
When parsing these tables, it can retrieve an assembly’s types and attributes.
Uses of Reflection:
when reflections are useful in C# are:
- 1. Reflections are quite useful for generating new types at runtime.
- 2. It is simple to use reflection with the program metadata attributes.
- 3. Reflection is needed to examine and instantiate types in the assembly.
- 4. For late binding to methods and properties, reflections are useful.
Applications of Reflection:
Reflection have the following applications −
- It allows viewing attribute data at runtime.
- It allows examining different types in an assembly and instantiating these types.
- It allows a late binding to methods and properties
- It allows generating new types at runtime and then performs some tasks using those types.
Advantages:
- Reflection increases the flexibility and scalability of the program, reduces coupling, and increases adaptive capabilities.
- It allows programs to make and control objects of any class without having to hard-code the target class in advance
Disadvantages:
1. Performance issues:
- The use of reflection is an interpretation operation, which is much slower than direct code when used for field and method access.
- Therefore, the reflection mechanism is majorly used in system frameworks that need high flexibility and scalability, and it is not recommended for ordinary programs.
2. The use of reflection will obscure the internal logic of a program:
- Programmers want to view the logic of the program in the source code, and reflection and other technologies that bypass the source code will cause maintenance problems
- The reflection code is much more complicated than the corresponding direct code.
- As for execution efficiency, because it is a strongly typed language with good execution efficiency.
- It is recommended to save the reflection in the cache.
- Although reflection is more powerful, it cannot be abused.
- If a function can be finished without reflection, it is best not to.
The first is a performance:
- Reflection contains some dynamic types, so the JVM cannot optimize these codes.
- Therefore, the efficiency of reflective operations is lower than those of non-reflective operations.
- We should avoid using reflection in frequently executed code or programs with more performance requirements.
Security:
- The use of reflection technology needs the program to run in an environment without security restrictions.
- If a program must run in an environment with security restrictions, like an Applet, then this is a problem.
Internal exposure of members:
- Since reflection permits the code to perform some operations that are not allowed under normal circumstances (such as accessing private properties and methods).
- Using reflection may cause unexpected side effects-the code has functional errors and reduces the portability.
- Reflective code destroys abstraction, so when the platform changes, the behavior of the code may also change
Viewing Metadata:
The MemberInfo object of the System.Reflection class requires it to be initialized for discovering the attributes associated with a class. To do this, define an object of the target class, as −
System.Reflection.MemberInfo info = typeof(MyClass);