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
13
votes
14 answers

Reflection. What can we achieve using it?

I'm reading and learning about reflection in C#. It would be fine to know how can it help me in my daily work, so I want people with more experience than me tell me samples or ideas about what kinds of things can we achieve using it, or how can we…
Jonathan
  • 11,809
  • 5
  • 57
  • 91
13
votes
1 answer

Creating dynamic type from TypeBuilder with a base class and additional fields generates an exception

I am trying to create a dynamic type based on an existing type that contains only public fields. The new dynamic type must also inherit from a different base type which only has a fully implemented method. I create the TypeBuilder specifying the…
dtaylor
  • 997
  • 3
  • 10
  • 26
13
votes
2 answers

Type casting using type parameter

Given is a Java method that returns java.lang.Objects for a given string. I'd like to wrap this method in a Scala method that converts the returned instances to some type T. If the conversion fails, the method should return None. I am looking for…
Fynn
  • 4,753
  • 3
  • 32
  • 69
13
votes
5 answers

Convert "C# friendly type" name to actual type: "int" => typeof(int)

I want to get a System.Type given a string that specifies a (primitive) type's C# friendly name, basically the way the C# compiler does when reading C# source code. I feel the best way to describe what I'm after is in the form of an unit-test. My…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
13
votes
4 answers

Accessing field of Protobuf message of unknown type in Python

Let's say I have 2 Protobuf-Messages, A and B. Their overall structure is similar, but not identical. So we moved the shared stuff out into a separate message we called Common. This works beautifully. However, I'm now facing the following problem: A…
djf
  • 6,592
  • 6
  • 44
  • 62
13
votes
2 answers

What's faster: expression trees or manually emitting IL

Is there a performance difference between creating a method emitting IL directly, as opposed to building an expression tree?
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
13
votes
4 answers

Reflection getAnnotations() returns null

Searchable.java @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Searchable { } Obj.java public class Obj { @Searchable String myField; } void main(String[] args) Annotation[] annotations =…
Menno
  • 12,175
  • 14
  • 56
  • 88
13
votes
2 answers

Does CallerMemberNameAttribute use reflection

You can use the CallerMemberName attribute to avoid specifying the member name as a String argument to the called method when implementing INotifyPropertyChanged interface. The question is does it use reflection behind the scene? Are there any…
ABCD
  • 897
  • 16
  • 38
13
votes
5 answers

Get read/write properties of Anonymous Type

I need to fetch all the properties of an anonymous type which can be written to. eg: var person = new {Name = "Person's Name", Age = 25}; Type anonymousType = person.GetType(); var properties = anonymousType.GetProperties(BindingFlags.Public |…
SharePoint Newbie
  • 5,974
  • 12
  • 62
  • 103
13
votes
4 answers

Preserving exceptions from dynamically invoked methods

Related Related I want to dynamically invoke a MethodInfo object and have any exceptions that get thrown from inside of it pass outward as if it were called normally. I have two options it seems. They're outlined below. Option 1 maintains the type…
Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
13
votes
7 answers

Get GenericType-Name in good format using Reflection on C#

I need to get the name of generic-type in form of its declaration in code. For example: For List I want to get string "List". Standart property Type.Name returns "List`1" in this situation. EDIT: example was fixed
AndreyAkinshin
  • 18,603
  • 29
  • 96
  • 155
13
votes
7 answers

Java : loop on all the classes in the classpath

Is there a way to iterate over all the classes in the classpath ? I want to make some reflective checks on some classes implementing a certain interface, but I want to do it completely dynamically, without any input on which classes to check, just…
glmxndr
  • 45,516
  • 29
  • 93
  • 118
13
votes
8 answers

How to find return type of a method in Java?

Could anyone help me find the return type of a method in Java. I tried this, but unfortunately it doesn't work. Please guide me. Method testMethod = master.getClass().getMethod("getCnt"); if (!"int".equals(testMethod.getReturnType())) { …
sunleo
  • 10,589
  • 35
  • 116
  • 196
13
votes
4 answers

Overriding a private method with Reflection

Is it possible to override a private method by using Reflection in .NET 3.5?
dr. evil
  • 26,944
  • 33
  • 131
  • 201
13
votes
3 answers

Using Reflection to determine which Fields are backing fields of a Property

I'm using reflection to map out objects. These objects are in managed code but I have no visibility into their source code, underlying structure, etc. other than through reflection. The overarching goal of all this is a rudimentary memory map of an…
nbelisle11
  • 172
  • 2
  • 10