This flow shows how to create an object, get its type info, then list and display its methods and properties.
Execution Sample
C Sharp (C#)
using System;
using System.Reflection;
var obj = "Hello";
var type = obj.GetType();
var methods = type.GetMethods();
var properties = type.GetProperties();
This code creates a string object, gets its type, then retrieves its methods and properties.
Execution Table
Step
Action
Evaluation
Result
1
Create object instance
obj = "Hello"
obj is a string object with value "Hello"
2
Get type info
type = obj.GetType()
type is System.String type info
3
Get methods list
methods = type.GetMethods()
methods is array of MethodInfo objects for System.String
4
Get properties list
properties = type.GetProperties()
properties is array of PropertyInfo objects for System.String
5
Display method names
foreach method in methods
Print method.Name for each method
6
Display property names
foreach property in properties
Print property.Name for each property
7
End
No more actions
Program ends
💡 All methods and properties of the object's type have been inspected and displayed.
Variable Tracker
Variable
Start
After Step 1
After Step 2
After Step 3
After Step 4
Final
obj
null
"Hello"
"Hello"
"Hello"
"Hello"
"Hello"
type
null
null
System.String
System.String
System.String
System.String
methods
null
null
null
MethodInfo[]
MethodInfo[]
MethodInfo[]
properties
null
null
null
null
PropertyInfo[]
PropertyInfo[]
Key Moments - 3 Insights
Why do we call GetType() on the object before getting methods or properties?
GetType() returns the type information of the object, which is needed to access its methods and properties via reflection, as shown in step 2 of the execution_table.
Are methods and properties the same thing when inspecting a type?
No, methods are actions the object can perform, while properties are data values it holds. They are retrieved separately using GetMethods() and GetProperties(), as seen in steps 3 and 4.
Why do we get arrays of MethodInfo and PropertyInfo instead of the actual methods or properties?
Reflection returns metadata objects (MethodInfo, PropertyInfo) describing methods and properties, not the actual executable code or data. This allows inspection without invoking them, as shown in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'type' after step 2?
Anull
B"Hello"
CSystem.String
DMethodInfo[]
💡 Hint
Check the 'type' variable value in variable_tracker after step 2.
At which step do we get the list of properties of the object?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for getting properties.
If we changed obj to an integer instead of a string, what would change in the execution?
AThe type variable would be System.Int32 instead of System.String
BThe methods array would be empty
CThe properties array would be null
DThe program would crash
💡 Hint
Consider what GetType() returns for different object types as shown in step 2.
Concept Snapshot
Inspecting methods and properties:
- Use obj.GetType() to get type info
- Use type.GetMethods() to get methods list
- Use type.GetProperties() to get properties list
- Methods and properties are metadata objects
- Useful for reflection and dynamic inspection
Full Transcript
This example shows how to inspect an object's methods and properties in C#. First, we create an object instance, here a string "Hello". Then we get its type information using GetType(). Next, we retrieve the list of methods with GetMethods() and the list of properties with GetProperties(). These return arrays of MethodInfo and PropertyInfo objects, which describe the methods and properties without executing them. We can then display their names or other details. This process is called reflection and helps us understand what an object can do or what data it holds at runtime.