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
5 answers

How to access a field's value via reflection (Scala 2.8)

Consider the following code: class Foo(var name: String = "bar") Now i try to get the value and the correct type of it via reflection: val foo = new Foo val field = foo.getClass.getDeclaredField("name") field.setAccessible(true) //This is where it…
soc
  • 27,983
  • 20
  • 111
  • 215
13
votes
4 answers

Why is it not possible to get local variable names using Reflection?

If I have a code like this: public class Program { public static void Main() { string bar = ""; int foo = 24; } } I can get the local variables declared in Main using: var flag = BindingFlags.Static |…
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
13
votes
5 answers

Unit testing that an event is raised in C#, using reflection

I want to test that setting a certain property (or more generally, executing some code) raises a certain event on my object. In that respect my problem is similar to Unit testing that an event is raised in C#, but I need a lot of these tests and I…
Thomas
  • 174,939
  • 50
  • 355
  • 478
13
votes
3 answers

newInstance() with inner classes

I've been working on an instantiation method that will allow me to package a variety of similar classes into one outer class. I could then instantiate each unique class type by passing the name of that type to the constructor. After a lot of…
Jonathon Anderson
  • 1,162
  • 1
  • 8
  • 24
13
votes
5 answers

C# Reflection - How to set field value for struct

How can I set value into struct field - myStruct.myField with reflection using DynamicMethod? When I call setter(myStruct, 111) value was not set, because MyStruct is value type. Console.WriteLine(myStruct.myField) shows value 3. How to modify…
Cyrus
  • 2,261
  • 2
  • 22
  • 37
13
votes
1 answer

Create generic delegate using reflection

I have the following code: class Program { static void Main(string[] args) { new Program().Run(); } public void Run() { // works Func> static_delegate = new…
Dathan
  • 7,266
  • 3
  • 27
  • 46
13
votes
3 answers

How can I tell using reflection if a class is final

Suppose I have a class: public final class Foo and a reflected Class clz reference that refers to that class. How can I tell (using clz) that Foo is final?
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
13
votes
3 answers

Is there a more efficient way to get an annotated method?

I started a "for fun, nobody knows, nobody cares" open source project (LinkSet). In one place I need to get an annotated method of a class. Is there a more efficient way to do it than this? I mean without the need of iterating through every…
Łukasz Bownik
  • 6,149
  • 12
  • 42
  • 60
13
votes
6 answers

Pros/Cons of using an assembly as a license file?

I was initially going to use a signed serialized xml file to store license details. In planning, more and more has moved into this "license file" which will allow for us to distribute a single application and control available functions via the…
Tim
  • 7,746
  • 3
  • 49
  • 83
13
votes
6 answers

Unable to cast object of type 'System.Object[]' to 'MyObject[]', what gives?

Scenario: I'm currently writing a layer to abstract 3 similar webservices into one useable class. Each webservice exposes a set of objects that share commonality. I have created a set of intermediary objects which exploit the commonality. However in…
Rob Stevenson-Leggett
  • 35,279
  • 21
  • 87
  • 141
13
votes
7 answers

Get type from GUID

For various reasons, I need to implement a type caching mechanism in C#. Fortunately, the CLR provides Type.GUID to uniquely identify a type. Unfortunately, I can't find any way to look up a type based on this GUID. There's Type.GetTypeFromCLSID()…
Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
13
votes
2 answers

GSON and InstanceCreator issue

I have the following POJOs: public interface Shape { public double calcArea(); public double calcPerimeter(); } public class Rectangle implement Shape { // Various properties of a rectangle } public class Circle implements Shape { …
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
13
votes
2 answers

Java Puzzle with reflection and String

This source outputs G'Day Mate. How this is happening ? public static void main(String args[]) { System.out.println("Hello World"); } static { try { Field value = String.class.getDeclaredField("value"); …
MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45
13
votes
2 answers

Java get generic type of collection

My project include reflection and i'm currently handling with generic types. I've managed to get the generic type from a List(java.util.List) with: if(originalField.getGenericType() instanceof ParameterizedType){ ParameterizedType pt =…
Gumba
  • 275
  • 1
  • 3
  • 10
13
votes
7 answers

How to get the type contained in a collection through reflection

In some part of my code I am passed a collection of objects of type T. I don't know which concrete colletion I will be passed, other than it impements IEnumerable. At run time, I need to find out which type T is (e.g. System.Double, System.String,…
Stefano Ricciardi
  • 2,923
  • 3
  • 33
  • 40