Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to display methods and properties using reflection in C#?
Reflection is the process of examining and describing the metadata of types, methods, and properties in a code at runtime. The System.Reflection namespace enables you to obtain data about loaded assemblies, the elements within them like classes, methods, and value types.
The most commonly used classes in System.Reflection include Assembly, AssemblyName, ConstructorInfo, MethodInfo, ParameterInfo, EventInfo, PropertyInfo, and MemberInfo.
Syntax
Following is the syntax for getting type information using reflection −
TypeInfo typeInfo = typeof(ClassName).GetTypeInfo(); IEnumerable<PropertyInfo> properties = typeInfo.DeclaredProperties; IEnumerable<MethodInfo> methods = typeInfo.DeclaredMethods;
Using Reflection to Display Properties and Methods
Reflection allows you to inspect the structure of a class at runtime, including its properties and methods. This is particularly useful for debugging, creating generic code, or building tools that analyze code structure −
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
class Program {
static void Main(string[] args) {
TypeInfo myType = typeof(TextInfo).GetTypeInfo();
IEnumerable<PropertyInfo> properties = myType.DeclaredProperties;
IEnumerable<MethodInfo> methods = myType.DeclaredMethods;
Console.WriteLine("Type: " + myType);
Console.WriteLine();
StringBuilder strBuilder = new StringBuilder();
strBuilder.Append("The properties are:");
foreach (PropertyInfo p in properties) {
strBuilder.Append("<br>" + p.Name);
}
strBuilder.Append("<br>\nThe methods are:");
foreach (MethodInfo m in methods) {
strBuilder.Append("<br>" + m.Name);
}
Console.WriteLine(strBuilder);
}
}
The output of the above code is −
Type: System.Globalization.TextInfo The properties are: Invariant ANSICodePage OEMCodePage MacCodePage EBCDICCodePage LCID CultureName IsReadOnly ListSeparator IsAsciiCasingSameAsInvariant IsRightToLeft The methods are: get_Invariant get_ANSICodePage get_OEMCodePage get_MacCodePage get_EBCDICCodePage get_LCID get_CultureName get_IsReadOnly get_ListSeparator set_ListSeparator get_IsAsciiCasingSameAsInvariant get_IsRightToLeft Clone ReadOnly ToLower ToUpper ToTitleCase Equals GetHashCode ToString
Using Reflection with Custom Classes
You can also use reflection to examine your own custom classes. This example shows how to display properties and methods of a custom Student class −
using System;
using System.Collections.Generic;
using System.Reflection;
public class Student {
public string Name { get; set; }
public int Age { get; set; }
public string Course { get; set; }
public void Study() {
Console.WriteLine("Student is studying");
}
public void TakeExam() {
Console.WriteLine("Student is taking exam");
}
public string GetInfo() {
return $"Name: {Name}, Age: {Age}, Course: {Course}";
}
}
class Program {
static void Main(string[] args) {
TypeInfo studentType = typeof(Student).GetTypeInfo();
IEnumerable<PropertyInfo> properties = studentType.DeclaredProperties;
IEnumerable<MethodInfo> methods = studentType.DeclaredMethods;
Console.WriteLine("Student Class Analysis:");
Console.WriteLine("=======================");
Console.WriteLine("\nProperties:");
foreach (PropertyInfo prop in properties) {
Console.WriteLine($"- {prop.Name} ({prop.PropertyType.Name})");
}
Console.WriteLine("\nMethods:");
foreach (MethodInfo method in methods) {
Console.WriteLine($"- {method.Name}");
}
}
}
The output of the above code is −
Student Class Analysis: ======================= Properties: - Name (String) - Age (Int32) - Course (String) Methods: - get_Name - set_Name - get_Age - set_Age - get_Course - set_Course - Study - TakeExam - GetInfo
Common Use Cases
-
Code Analysis Tools: Building tools that examine code structure and generate documentation.
-
Serialization: Converting objects to/from formats like JSON or XML by inspecting properties.
-
Dependency Injection: Creating instances and setting properties dynamically.
-
Unit Testing: Accessing private members or verifying class structure.
Conclusion
Reflection in C# provides powerful capabilities to examine type metadata at runtime. Using TypeInfo, PropertyInfo, and MethodInfo classes, you can dynamically discover and analyze the structure of any class, making it invaluable for building flexible and generic applications.
