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
12
votes
4 answers

Python reflection - Can I use this to get the source code of a method definition

Duplicate of.. How can I get the code of python function? print the code which defined a lambda function Python: How do you get Python to write down the code of a function it has in memory? I have a method definition which is successfully…
Sathya Murali
  • 149
  • 2
  • 6
12
votes
2 answers

How to get arguments list of a built-in Python class constructor?

I'm trying to use the inspect module, however it seems I can't use it on a built-in (native?) class, or else I misunderstood. I'm using Python 2.7 and tried with Python 3.2. This is working: >>> import inspect >>> class C: ... def…
Julian Bilcke
  • 309
  • 1
  • 4
  • 15
12
votes
2 answers

Can I add new fields in a Class using reflection

Can I add a new field to a class if I have its class literal object and how can I determine that a particular Class is referenced or used in that class literal ?
Chandan
  • 229
  • 1
  • 5
  • 14
12
votes
2 answers

Class introspection in Common Lisp

Java's java.lang.Class class has a getDeclaredFields method which will return all the fields in a given class. Is there something similar for Common Lisp? I came across some helpful functions such as describe, inspect and symbol-plist after reading…
Bala
  • 979
  • 1
  • 10
  • 21
12
votes
3 answers

How to check if a parameter of the current method has an annotation and retrieve that parameter value in Java?

Consider this code: public example(String s, int i, @Foo Bar bar) { /* ... */ } I want to check if the method has an annotation @Foo and get the argument or throw an exception if no @Foo annotation is found. My current approach is to first get…
soc
  • 27,983
  • 20
  • 111
  • 215
12
votes
3 answers

Java reflection run-time performance

This is an academical exercise (disclaimer). I'm building an application that will profit from being as fast as possible as it will compete with others. I know that declaring a class using reflection (example below) will suffer a huge penalty as…
Frankie
  • 24,627
  • 10
  • 79
  • 121
12
votes
4 answers

How to make sure controller and action exists before doing redirect, asp.net mvc3

In one of my controller+action pair, I am getting the values of another controller and action as strings from somewhere and I want to redirect my current action. Before making a redirect I want to make sure that controller+action exists in my app,…
Rusi Nova
  • 2,617
  • 6
  • 32
  • 48
12
votes
1 answer

What is the order of returned Types by Assembly.GetTypes()?

If I get the list of types in my AppDomain, is there an inherent ordering to these types? List myTypes = new List(); foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) myTypes.AddRange(a.GetTypes()); This seems to…
Michael
  • 3,099
  • 4
  • 31
  • 40
12
votes
4 answers

Getting collection of all members of a class

I want to get the collection of all the members that are present in a class. How do I do that? I am using the following, but it is giving me many extra names along with the members. Type obj = objContactField.GetType(); MemberInfo[] objMember =…
12
votes
2 answers

C# Create lambda over given method that injects first paramater

In C# I have following methods defined in given class (non static): int MyMethod(ScriptEngine script, int a, int b) { return a + b; } void MyMethod2(ScriptEngine script, string c) { // do something with c } I want to create wrapping lambda…
PiotrK
  • 4,210
  • 6
  • 45
  • 65
12
votes
2 answers

IllegalAccessException when using call dynamic methods

I am trying to use Reflection in Java but I am getting a weird error. What are the possible issues when I get an error that says: java.lang.IllegalAccessException: Class com.myapp.core.utils.EventDispatcher can not access a member of class appApp$1…
Carven
  • 14,988
  • 29
  • 118
  • 161
12
votes
5 answers

Forced Garbage collection or reflection to a Private field, which is less evil?

We have a third party library that internally uses a SafeHandle to an unmanaged resource. In some error cases it is necessary to dispose of the object and recreate it. But, there is a bug in the dispose implementation that prevents the Handle from…
Yaur
  • 7,333
  • 1
  • 25
  • 36
12
votes
2 answers

java reflection getFields for private member| accessing object name value dynamically

I want to print all of the class's properties with their name and values. I have used reflection, but getFields give me length of 0. RateCode getMaxRateCode = instance.getID(Integer.parseInt((HibernateUtil …
Ravi Parekh
  • 5,253
  • 9
  • 46
  • 58
12
votes
3 answers

Using Kotlin reflection sealedSubClasses property in Android release build

When I am trying to use the sealedSubClasses attribute of a reified class in Kotlin, it only works in my debug and not in my release build. I guess this is a problem with ProGuard, but I don't know how to fix this. I already tried keeping all…
sunilson
  • 1,449
  • 15
  • 31
12
votes
3 answers

Get collection of values from struct's const properties

I've got a struct that looks like this: public struct MyStruct { public const string Property1 = "blah blah blah"; public const string Property2 = "foo"; public const string Property3 = "bar"; } I want to programmatically retrieve a…
SquidScareMe
  • 3,108
  • 2
  • 24
  • 37