What if your program could discover and use new features all by itself, without you rewriting code?
Why reflection is needed in C Sharp (C#) - The Real Reasons
Imagine you have a big box of different tools, but you don't know what each tool is or what it does. You want to use the right tool, but you have to open the box and check each one manually every time.
Manually checking each tool is slow and tiring. If you add new tools, you have to update your list every time. It's easy to make mistakes or miss something important, especially when the tools change often.
Reflection lets your program look inside itself or other objects to find out what tools (properties, methods) they have, without needing a fixed list. It's like having a smart helper who can quickly identify and use the right tool automatically.
if (obj is Car) { var speed = ((Car)obj).Speed; } else if (obj is Bike) { var speed = ((Bike)obj).Speed; }
var speedProp = obj.GetType().GetProperty("Speed"); var speed = speedProp?.GetValue(obj);Reflection enables programs to adapt and work with unknown or changing data and types dynamically, making them more flexible and powerful.
Think of a photo app that can open many image formats without knowing them all in advance. Reflection helps it discover and use the right tools to handle each format on the fly.
Manual checking of types and properties is slow and error-prone.
Reflection allows programs to inspect and use objects dynamically.
This makes software more flexible and easier to maintain.
Practice
Solution
Step 1: Understand what reflection does
Reflection allows a program to look at its own structure, like classes and methods, while it is running.Step 2: Identify the purpose of reflection
It is used to examine and interact with types dynamically, not for speed or UI creation.Final Answer:
To examine and use information about types at runtime -> Option CQuick Check:
Reflection = runtime type info [OK]
- Confusing reflection with UI design
- Thinking reflection speeds up code
- Believing reflection manages memory
obj using reflection in C#?Solution
Step 1: Recall the method to get an object's type
In C#, the methodGetType()is called on the object instance to get its type.Step 2: Check each option's syntax
Type t = obj.GetType(); usesobj.GetType(), which is correct. Type t = GetType(obj); and D are invalid syntax. Type t = typeof(obj); usestypeofincorrectly because it expects a type name, not an object.Final Answer:
Type t = obj.GetType(); -> Option AQuick Check:
obj.GetType() gets type [OK]
- Using typeof with an object instance
- Calling non-existent methods like obj.Type()
- Trying to pass object to GetType as argument
using System;
class Program {
static void Main() {
var type = typeof(string);
Console.WriteLine(type.Name);
}
}Solution
Step 1: Understand typeof operator
typeof(string)returns the Type object for the string class.Step 2: Check what
Thetype.NamereturnsNameproperty returns the class name without namespace, which is "String" with capital S.Final Answer:
String -> Option BQuick Check:
typeof(string).Name = "String" [OK]
- Confusing lowercase 'string' keyword with class name
- Expecting full namespace in Name property
- Thinking output is 'Type'
Type t = typeof(int);
var method = t.GetMethod("Parse");
int result = method.Invoke(null, new object[] { "123" });
Console.WriteLine(result);Solution
Step 1: Check method retrieval
int(System.Int32) has a static method "Parse", soGetMethod("Parse")works.Step 2: Check Invoke return type and usage
Invokereturns an object. Assigning it directly to int causes a compile error. It needs explicit cast:(int)method.Invoke(...).Final Answer:
Invoke returns object, needs casting to int -> Option AQuick Check:
Invoke returns object, cast needed [OK]
- Ignoring cast from object to int
- Thinking GetMethod("Parse") is invalid
- Assuming typeof(int) is wrong
Run without knowing the class name beforehand. Which reflection approach is best?Solution
Step 1: Understand dynamic plugin loading
When class name is unknown, you must load assembly dynamically and inspect types.Step 2: Use reflection to find and invoke method
Assembly.Load loads the plugin. GetTypes() lists classes. Then you find the class with Run method and invoke it.Final Answer:
Use Assembly.Load, then find type with GetTypes(), then invoke Run method -> Option DQuick Check:
Dynamic loading needs Assembly.Load + reflection [OK]
- Using typeof() without knowing class name
- Trying to instantiate class by string name directly
- Calling method without loading assembly
Ready to prove it?
Pass the test and this pattern moves unknown
