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
2527
votes
24 answers

What is reflection and why is it useful?

What is reflection, and why is it useful? I'm particularly interested in Java, but I assume the principles are the same in any language.
Lehane
  • 47,588
  • 14
  • 53
  • 53
2385
votes
17 answers

Calling a function of a module by using its name (a string)

How do I call a function, using a string with the function's name? For example: import foo func_name = "bar" call(foo, func_name) # calls foo.bar()
ricree
  • 35,626
  • 13
  • 36
  • 27
1280
votes
9 answers

How do I use reflection to call a generic method?

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the Example() method, what's the most concise way to invoke…
Bevan
  • 43,618
  • 10
  • 81
  • 133
1230
votes
32 answers

How to create a generic array in Java?

Due to the implementation of Java generics, you can't have code like this: public class GenSet { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation } } How can I implement…
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40
1167
votes
24 answers

Get property value from string using reflection

I am trying implement the Data transformation using Reflection1 example in my code. The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties and have GetSourceValue get the value of the…
pedrofernandes
  • 16,354
  • 10
  • 36
  • 43
926
votes
31 answers

How do I get the path of the assembly the code is in?

Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code. Basically my unit test needs to read some xml test files which are located…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
913
votes
11 answers

How to create a new object instance from a Type

One may not always know the Type of an object at compile-time, but may need to create an instance of the Type. How do you get a new object instance from a Type?
tags2k
  • 82,117
  • 31
  • 79
  • 106
770
votes
11 answers

How to get the list of properties of a class?

How do I get a list of all the properties of a class?
Sukesh
767
votes
23 answers

How do I invoke a Java method when given the method name as a string?

If I have two variables: Object obj; String methodName = "getName"; Without knowing the class of obj, how can I call the method identified by methodName on it? The method being called has no parameters, and a String return value. It's a getter for…
brasskazoo
  • 76,030
  • 23
  • 64
  • 76
706
votes
16 answers

How to determine if a type implements an interface with C# reflection

Does reflection in C# offer a way to determine if some given System.Type type models some interface? public interface IMyInterface {} public class MyType : IMyInterface {} // should yield 'true' typeof(MyType)./* ?????…
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
663
votes
11 answers

C# getting its own class name

If I have a class called MyProgram, is there a way of retrieving "MyProgram" as a string?
deltanovember
  • 42,611
  • 64
  • 162
  • 244
656
votes
19 answers

Getting all types that implement an interface

Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations? This is what I want to re-write: foreach (Type t in this.GetType().Assembly.GetTypes()) if (t is…
juan
  • 80,295
  • 52
  • 162
  • 195
650
votes
30 answers

Get generic type of class at runtime

How can I achieve this? public class GenericClass { public Type getMyType() { //How do I return the type of T? } } Everything I have tried so far always returns type Object rather than the specific type used.
Glenn
  • 6,525
  • 3
  • 16
  • 3
642
votes
29 answers

Can you find all classes in a package using reflection?

Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)
Jonik
  • 80,077
  • 70
  • 264
  • 372
607
votes
15 answers

Checking if a variable is defined?

How can I check whether a variable is defined in Ruby? Is there an isset-type method available?
readonly
  • 343,444
  • 107
  • 203
  • 205
1
2 3
99 100