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

How to find the smallest assignable type in two types (duplicate)?

Here're two extension methods for use public static Type FindInterfaceWith(this Type type1, Type type2) { // returns most suitable common implemented interface } public static Type FindBaseClassWith(this Type type1, Type type2) { // returns…
Ken Kin
  • 4,503
  • 3
  • 38
  • 76
13
votes
4 answers

C# Reflection - Base class static fields in Derived type

In C#, when I'm reflecting over a derived type, how come I don't see base classes' static fields? I've tried both type.GetFields(BindingFlags.Static) and type.GetFields().
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
13
votes
1 answer

How can I get the executing assembly version information in a Windows Store App?

While porting an application to the Windows Store, I noticed the .NETCore Framework does not include: System.Reflection.Assembly.GetExecutingAssembly() I used this to get the version information for display on the menu screen. Is there a replacement…
Justin Skiles
  • 9,373
  • 6
  • 50
  • 61
13
votes
2 answers

How to get properties of a class in WinRT

I am writing a Windows 8 application in C# and XAML. I have a class with many properties of the same type that are set in the constructor the same way. Instead of writing and assignment for each of the properties by hand I want to get a list of all…
Igor Kulman
  • 16,211
  • 10
  • 57
  • 118
13
votes
4 answers

Can I create a new function using reflection in Go?

I have an idea to use interfaces in Go to define RPC style interfaces. So for a given service, I might create an interface like this: type MyService interface{ Login(username, password string) (sessionId int, err error) HelloWorld(sessionId int)…
Matt
  • 1,424
  • 10
  • 15
13
votes
3 answers

App config for dynamically loaded assemblies

I'm trying to load modules into my application dynamically, but I want to specify separate app.config files for each one. Say I have following app.config setting for main app:
Grozz
  • 8,317
  • 4
  • 38
  • 53
13
votes
1 answer

Mutable strings in Java

As almost everybody knows strings in Java are immutable. Recently I discovered something that might suggest that it's not always true. Let's try out this code: System.out.println("-------- BEFORE MODIFICATIONS --------"); String beforeTest = new…
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
13
votes
3 answers

Avoiding user code calling to Reflection in C#

I'm implementing an automatic "evaluator" for a course I'm currently teaching. The overall idea is that every student delivers a DLL with some algorithms implemented. My evaluator loads all these DLLs using Reflection, finds the student…
Alejandro Piad
  • 1,827
  • 1
  • 16
  • 23
13
votes
3 answers

C# reflection: If ... else?

I'm currently facing new problem with operators. Using following code, I want to make output that would be same as when using if ... else pair in C#. var method = new DynamicMethod("dummy", null, Type.EmptyTypes); var g =…
user35443
  • 6,309
  • 12
  • 52
  • 75
13
votes
6 answers

Generate HTML table from list of generic class with specified properties

I want to generate an HTML table from a couple specified parameters. Specifically, the two parameters I want to pass into my method are: IEnumerable list, and some subset of properties of T. For example, let's say I have a List of this class: class…
birdus
  • 7,062
  • 17
  • 59
  • 89
13
votes
2 answers

Referencing current assembly with CompilerParameters

Right now I'm working on a project, and the team wants a way to write code and edit it without having to recompile the whole project, so I've decided to try and implement a scripting engine. Having implemented Lua into C++ before, I wasn't an entire…
Dan
  • 10,282
  • 2
  • 37
  • 64
13
votes
4 answers

Change default arguments of an R function at runtime

Is it possible to change the default values of formal parameters in an R function at runtime? Let's assume, we have the function f <- function(x=1) { ... } can I somehow change the default value of x from 1 to, say, 2? Thanks in advance, Sven
Sven Hager
  • 3,144
  • 4
  • 24
  • 32
13
votes
2 answers

Dynamically create type and call constructor of base-class

I need to create a class dynamically. Most things work fine but i'm stuck in generating the constructor. AssemblyBuilder _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyBuilder"), …
Daniel Bişar
  • 2,663
  • 7
  • 32
  • 54
12
votes
3 answers

Is possible iterate over all classes inside a package with Reflection?

I have a String with some value. I want to iterate over all classes in a package calling a specific method, and if the value returned by the method is equals the value in my String, create an object of that class. I want do this in a dynamic way, so…
Renato Dinhani
  • 35,057
  • 55
  • 139
  • 199
12
votes
1 answer

Prevent changing the value of String.Empty

Partially from a curious breaking things point of view and partially from a safeguarding against potential problems. Imagine what is the worst that can happen by calling the following (or something similar, but string.Empty is a good…
Seph
  • 8,472
  • 10
  • 63
  • 94