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

this.getClass().getFields().length; always returns 0

I am trying to get the number of fields in a particular class. However the technique I am using is not working and always returns 0: this.getClass().getFields().length; How do I get the field count of a particular class?
Roy Hinkley
  • 10,111
  • 21
  • 80
  • 120
14
votes
2 answers

Create instance of unknown Enum with string value using reflection in C#

I have a problem working out how exactly to create an instance of an enum when at runtime i have the System.Type of the enum and have checked that the BaseType is System.Enum, my value is an int value matching an item in the mystery Enum. The code…
Jarmez De La Rocha
  • 618
  • 2
  • 9
  • 19
14
votes
3 answers

How does reflection and immutability supposed to work together

According to JSR-133 immutable objects are thread safe and don't need synchronization. However it's possible to update values of final fields using reflection: package com.stackoverflow; import java.lang.reflect.Field; public class WhatsGoingOn…
Petro Semeniuk
  • 6,970
  • 10
  • 42
  • 65
14
votes
6 answers

Set property Nullable<> by reflection

I try to set a Nullable<> property dynamicly. I Get my property ex : PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int I want to set my property by reflection…
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98
14
votes
5 answers

Possible Solutions to Poor Serialization Performance

I recently did some performance testing and analysis of an ASP.NET application using out-of-process session state - this is necessary when using session state on a web farm so that state can be retrieved on any of the web servers, e.g. if subsequent…
redcalx
  • 8,177
  • 4
  • 56
  • 105
14
votes
2 answers

java.lang.NoClassDefFoundError: org/omg/CORBA/InterfaceDef

I am seeing java.lang.NoClassDefFoundError: org/omg/CORBA/InterfaceDef when I am calling the method getMethod of the java.lang.Class. I am trying to get the getEmsSession method in EmsSeeionFactory_I of TMF specific jars using reflection. But when I…
sakthisundar
  • 3,278
  • 3
  • 16
  • 29
14
votes
3 answers

EF5 ObjectContext : How to replace IQueryable.Include(Path) with context.T.Attach()

I'm using Entity Framework 5 with ObjectContext on a relatively big and complex data model. I would like to work around big queries generated when chaining multiple IQueryable.Include(Path) to eager load related objects. For example, I'm doing…
anon
14
votes
3 answers

Using Attributes to Call Methods

I have various individual methods which all need to perform the same functions before continuing on with their own implementation. Now I could implement these functions in each method, but I was wondering if there's a way to exploit attributes to do…
keyboardP
  • 68,824
  • 13
  • 156
  • 205
14
votes
6 answers

How to get Names of DLLs used by application

I'm looking the way to read all assemblies (.dlls) used by my app. In a standard C# project there is "References" folder, when it is expanded I can read all libraries used. My goal is programatically read all assemblies which are used by each…
Maciej
  • 10,423
  • 17
  • 64
  • 97
14
votes
4 answers

Debug dynamically loaded assembly in Visual Studio .NET

I am using C# and reflection to load and invoke methods from an assembly. I have the source code of the assembly itself. What do I need to do to get the debugger to step into (and not over) the code of the dynamically loaded assembly ? If I press…
G-Man
  • 7,232
  • 18
  • 72
  • 100
14
votes
5 answers

Is it possible to call value type operators via reflection?

As C# operators e.g. +, +=, == are overridable. It lets me think they are sort of methods, thus wonder if there is a way to call them using reflection, on Int32 for instance.
Sébastien Ros - MSFT
  • 5,091
  • 29
  • 31
14
votes
4 answers

Loading assemblies at runtime and creating instances using Activator.CreateInstance()

I am trying to load an assembly at runtime, and I'm unsure as to why I can't create an instance of a type in the assembly using the static Activator.CreateInstance(). It works with Assembly.CreateInstance(). string assemblyFilename =…
Dave New
  • 38,496
  • 59
  • 215
  • 394
14
votes
1 answer

Determining if a method overrides another at runtime

I was wondering if there was any way to determine if a method represented by given java.lang.Method object overrides another methods represented by another java.lang.Method object? I'm working on Stronlgy typed javascript, and I need to be able to…
LordOfThePigs
  • 11,050
  • 7
  • 45
  • 69
14
votes
3 answers

What is the difference between Mirror based reflection and traditional reflection?

Some languages like Dart use mirror based reflection so, in simple terms, what is the difference between such implementation and traditional reflection as you see in C# or Java. Update: I found this excellent (and somewhat quirky) video by Gilad…
14
votes
4 answers

Ignoring Version in an assembly-qualified name passed to Type.GetType()

Is it possible to get a Type via Type.GetType() when the assembly-qualified name passed into GetType() specifies a different Version than the version of the DLL that's actually loaded? If so, what is the behavior of GetType()? I want to get a Type…
sourcenouveau
  • 29,356
  • 35
  • 146
  • 243