Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.
Questions tagged [reflection]
52 questions
50
votes
4 answers
Why is it a bad idea to create a generic setter and getter with reflection?
A while back I wrote this answer to a question on how to avoid having a getter and setter for every mutable variable. At the time, I had only a hard-to-verbalize gut feeling that this is a Bad Idea, but OP was explicitly asking how to do it. I…

HAEM
- 503
- 1
- 5
- 12
36
votes
6 answers
Why should I use reflection?
I am new to Java; through my studies, I read that reflection is used to invoke classes and methods, and to know which methods are implemented or not.
When should I use reflection, and what is the difference between using reflection and…

Hamzah khammash
- 481
- 1
- 4
- 5
29
votes
3 answers
Reflection: Is using reflection still "bad" or "slow"? What has changed with reflection since 2002?
I've noticed when dealing with Expressions or Expression Trees I'm using reflection a lot to set and get values in properties and what have you. It has occurred to me that the use of reflection seems to be getting more and more common. Things like…

blesh
- 899
- 1
- 9
- 15
28
votes
5 answers
Is Java instanceof operator considered reflection, and what defines reflection?
I had a discussion with a coworker today, whether usage of using the Java operator instanceof is a kind of reflection. And the discussion quickly evolved into what actually defines reflection.
So, what is the definition of reflection?
And is the…

Bjarke Freund-Hansen
- 1,176
- 1
- 11
- 18
24
votes
6 answers
Is it a bad habit to (over)use reflection?
Is it a good practice to use reflection if greatly reduces the quantity of boilerplate code?
Basically there is a trade-off between performance and maybe readability on one side and abstraction/automation/reduction of boilerplate code on the other…

Random42
- 10,370
- 10
- 48
- 65
20
votes
4 answers
Best practice to mark a method that is called via reflection?
Our software has several classes that should be dynamically found via reflection. The classes all have a constructor with a specific signature via which the reflection code instantiates objects.
However, when someone checks whether the method is…

Kasper van den Berg
- 2,636
- 1
- 16
- 32
18
votes
4 answers
What do IDEs use to do code completion suggestions?
For example if I have a class like
class Foo {
public int bar;
public Foo(int constructor_var) {
bar = construction_var;
}
public bar_plus_one() {
return bar++;
}
}
Foo foo = new Foo(2);
and in the IDE I type…

ewhiting
- 329
- 2
- 11
14
votes
6 answers
Is Reflection a disadvantage as private variables cannot be restricted?
The private modifier is used to restrict access outside the class, but using reflection other classes can access private method and fields. So I am wondering how we can restrict accessibility if it is part of requirement.

user245930
- 175
- 1
- 3
13
votes
3 answers
How to design a C++ program to allow for runtime import of functions?
today, I like to ask you a question towards the capabilities of C++ to realize a specific software architecture.
Of course, I have used the search but have not found any directly linked answer.
Basically, my goal is to build a program which allows…

Oliver
- 157
- 1
- 5
12
votes
1 answer
What's the relationship between meta-circular interpreters, virtual machines and increased performance?
I've read about meta-circular interpreters on the web (including SICP) and I've looked into the code of some implementations (such as PyPy and Narcissus).
I've read quite a bit about two languages which made great use of metacircular evaluation,…

Gomi
- 332
- 1
- 4
12
votes
3 answers
Do I need to deal with the situation where private methods are called through reflection?
When creating a library, must I ensure that the private methods must work as expected when called not by other methods of the same class, but by another library through reflection?
For example, if a private method private DoSomething(int number)…

Arseni Mourzenko
- 134,780
- 31
- 343
- 513
7
votes
2 answers
Are there any reliable solutions for annotations/reflection/code-metadata in C?
Not all languages support java-like annotations or C#-like attributes or code metadata in general, however that doesn't mean it is not possible to have in languages that don't have this.
An example is PHP with Stubbles and the Doctrine annotation…

dukeofgaming
- 13,943
- 6
- 50
- 77
6
votes
6 answers
In creating a "registry", which is worse: using reflection or violating open/closed principle?
In my current software engineering course, my team is working on a library management system that is essentially a command-line/REPL environment with a half dozen commands, e.g. checkout, search, etc. We've elected to use the command pattern for…

The Guy with The Hat
- 512
- 4
- 12
6
votes
2 answers
assembly.GetTypes() vs assembly.DefinedTypes.Select(t => t.AsType());
public static IEnumerable GetAccessibleTypes(this Assembly assembly)
{
try
{
#if NET40
return assembly.GetTypes();
#else
return assembly.DefinedTypes.Select(t => t.AsType());
#endif
}
…

Oğuzhan Topçu
- 181
- 1
- 3
6
votes
4 answers
Is it bad programming practice to check if a class referenced by its interface is an instance of another class?
I have a class (Timer) with an array list of Timable objects. Timeable is an interface. There is some specific functionality that I need for the Trigger class (implements Timable), which has a reference called target. A lot of methods need to…

sinθ
- 1,311
- 15
- 25