Reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.
Questions tagged [reflection]
55 questions
51
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
- 513
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
31
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
- 919
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,186
27
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,520
- 10
- 52
- 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,666
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
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
13
votes
6 answers
Why use a special "Name" class (instead of just a string) for representing object names in C++?
Suppose we have an Instance class in a C++ program, which has a GUID/UUID, name, parents, children, and other properties which can be saved to or loaded from an XML file.
The intuitive approach for representing the name of the Instance is to give it…
Bunabyte
- 643
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
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
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
- 137,583
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
- 14,023
- 6
- 52
- 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…
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