Questions tagged [reflection]

Reflection is the ability of a program to observe and/or modify its structure and/or behavior at runtime. Reflection is dependent on the supporting programming language - please tag the programming language being used when using this tag.

Overview

Reflection is an ability of a program to perform introspection on itself. This usually involves observing and/or modifying its structure and behavior at runtime.

From a theoretical perspective, reflection relates to the fact that program instructions are stored as data. The distinction between program code and data is a matter of how the information is interpreted, and thus is, in fact, arbitrary. Hence, a program can treat its own code as data and observe or modify it.

Caution should be used when using reflection - the modification of a program's entity during runtime can lead to hard to detect bugs which are generally severe.

Popular questions:

Language-specific implementations

Java:

C#:

Python:

Scala:

See also

24902 questions
592
votes
15 answers

Programmatic equivalent of default(Type)

I'm using reflection to loop through a Type's properties and set certain types to their default. Now, I could do a switch on the type and set the default(Type) explicitly, but I'd rather do it in one line. Is there a programmatic equivalent of…
tags2k
  • 82,117
  • 31
  • 79
  • 106
588
votes
4 answers

What are the use(s) for struct tags in Go?

In the Go Language Specification, it mentions a brief overview of tags: A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. The tags are…
liamzebedee
  • 14,010
  • 21
  • 72
  • 118
578
votes
20 answers

How to list all functions in a module?

I have a Python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. I want to call the help function on each one. In Ruby I can do something like ClassName.methods to get a list of all the…
Chris Gow
  • 7,514
  • 4
  • 23
  • 18
571
votes
27 answers

Getting attributes of Enum's value

I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum: using System.ComponentModel; // for DescriptionAttribute enum FunkyAttributesEnum { …
Alex K
  • 10,835
  • 8
  • 29
  • 34
564
votes
14 answers

Change private static final field using Java reflection

I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the…
fixitagain
  • 6,543
  • 3
  • 20
  • 24
557
votes
14 answers

How to read the value of a private field from a different class in Java?

I have a poorly designed class in a 3rd-party JAR and I need to access one of its private fields. For example, why should I need to choose private field is it necessary? class IWasDesignedPoorly { private Hashtable…
Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
547
votes
26 answers

How do I get the path and name of the python file that is currently executing?

I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process. For example, let's say I have three files. Using execfile: script_1.py calls script_2.py. In turn, script_2.py…
Ray
  • 187,153
  • 97
  • 222
  • 204
533
votes
23 answers

Getting the name of the currently executing method

Is there a way to get the name of the currently executing method in Java?
Omar Kooheji
  • 54,530
  • 68
  • 182
  • 238
506
votes
15 answers

What is the difference between instanceof and Class.isAssignableFrom(...)?

Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only difference that I know of is, when 'a' is null, the first returns false, while the second throws an exception. Other than that, do they always give…
Megamug
  • 6,277
  • 5
  • 21
  • 13
428
votes
32 answers

Get class name of object as string in Swift

Getting the classname of an object as String using: object_getClassName(myViewController) returns something like this: _TtC5AppName22CalendarViewController I am looking for the pure version: "CalendarViewController". How do I get a cleaned up…
Bernd
  • 11,133
  • 11
  • 65
  • 98
419
votes
14 answers

Test if object implements interface

What is the simplest way of testing if an object implements a given interface in C#? (Answer to this question in Java)
JoshRivers
  • 9,920
  • 8
  • 39
  • 39
419
votes
10 answers

Open Source Alternatives to Reflector?

Just to ask if anyone knows of an open source alternative to RedGate's Reflector? I'm interested in checking out how a tool similar to Reflector actually works. Note, if you know of a free but not open source alternative to Reflector, you can…
Tangiest
  • 43,737
  • 24
  • 82
  • 113
413
votes
16 answers

Is a Java string really immutable?

We all know that String is immutable in Java, but check the following code: String s1 = "Hello World"; String s2 = "Hello World"; String s3 = s1.substring(6); System.out.println(s1); // Hello World System.out.println(s2); // Hello World …
Darshan Patel
  • 3,176
  • 6
  • 26
  • 49
411
votes
13 answers

How can I get a list of all classes within current module in Python?

I've seen plenty of examples of people extracting all of the classes from a module, usually something like: # foo.py class Foo: pass # test.py import inspect import foo for name, obj in inspect.getmembers(foo): if inspect.isclass(obj): …
mcccclean
  • 7,721
  • 10
  • 32
  • 36
406
votes
5 answers

How do I check if a type is a subtype OR the type of an object?

To check if a type is a subclass of another type in C#, it's easy: typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true However, this will fail: typeof (BaseClass).IsSubclassOf(typeof (BaseClass)); // returns false Is there any way…
Daniel T.
  • 37,212
  • 36
  • 139
  • 206