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
1 answer

What is the purpose of the methods in System.Reflection.RuntimeReflectionExtensions?

Since .NET 4.5 (2012), some new extension methods show up, from System.Reflection.RuntimeReflectionExtensions class. However, the new methods do not seem to give us anything new. An example: static void Main() { var prop1 =…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
14
votes
2 answers

Printing debug info on errors with java 8 lambda expressions

I want to use a static method as setter helper that catch exceptions and print debug info about the operation that failed. I don't want the exception details only. I want to show what property was being set so that detail help to debug the problem…
aalku
  • 2,860
  • 2
  • 23
  • 44
14
votes
2 answers

Reflecting over all properties of an interface, including inherited ones?

I have an instance of System.Type that represents an interface, and I want to get a list of all the properties on that interface -- including those inherited from base interfaces. I basically want the same behavior from interfaces that I get for…
Joe White
  • 94,807
  • 60
  • 220
  • 330
14
votes
4 answers

Scala equivalent to C#'s Expression API

Is their an equivalent to C#'s Expression API in scala? For example, I would like to have a lambda like this: (Foo) => Foo.bar and be able to access "bar" in the function it is passed to.
user281655
  • 467
  • 1
  • 3
  • 11
14
votes
4 answers

the internals of System.String

I used reflection to look at the internal fields of System.String and I found three fields: m_arrayLength m_stringLength m_firstChar I don't understand how this works. m_arrayLength is the length of some array. Where is this array? It's…
BowserKingKoopa
  • 2,701
  • 3
  • 33
  • 40
14
votes
6 answers

Reflection can't find private setter on property of abstract class

When I have this property in an abstract class: public IList Components { get; private set; } Then when I call: p.GetSetMethod(true) with p being a PropertyInfo object pointing to my property, I get null. However if I change the…
ekolis
  • 6,270
  • 12
  • 50
  • 101
14
votes
4 answers

What is the most efficient way to add a reflection to a UIImageView

I just want the easiest way to make a reflection under a UIImageVies that is easily managable.
Jab
  • 26,853
  • 21
  • 75
  • 114
14
votes
4 answers

How to extend class with an extra property

Suppose I've got a class named Foo. I cannot change the Foo class but I wan't to extend it with a property named Bar of type string. Also I've got a lot more classes like Foo so I'm interested in a 'generic' solution. I'm looking into…
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
14
votes
3 answers

Invoke private method with java.lang.invoke.MethodHandle

How can I invoke private method using method handles ? As far as I can see there are only two kinds of publicly accessible Lookup instances: MethodHandles.lookup() MethodHandles.publicLookup() and neither allows unrestricted private access. There…
Marcin Wisnicki
  • 4,511
  • 4
  • 35
  • 57
14
votes
4 answers

Getting java.lang.NullPointerException when calling Method.invoke

I'm following this tutorial on Java annotaitons and implemented the Test annotation as shown there. But when running the code I get the following output. java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native…
Can't Tell
  • 12,714
  • 9
  • 63
  • 91
14
votes
2 answers

How to serialize ANY Object into a URI?

My basic question: is there anything built that already does this automatically (doesn't have to be part of a popular library/package)? The main things I'm working with are Spring (MVC) and Jackson2. I understand there are a few manual ways to do…
Ian
  • 50,146
  • 13
  • 101
  • 111
14
votes
3 answers

Unable to resolve assembly Model.dll

I had a class library project that used Entity Framework and it worked fine until I moved the model out to a separate class library type project. After I moved the model out to a separate class library project called Model, I changed the connection…
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
14
votes
4 answers

Is it possible to speed this method up?

I have a method that uses loops through 7,753+ objects and Gets the value of each property for each object. Each object has 14 properties. private void InitializeData(IList objects, PropertyInfo[] props, List dataPs, List
Xaisoft
  • 45,655
  • 87
  • 279
  • 432
14
votes
3 answers

Loading an assembly by Bytes loses the location

I want to load the assembly via the following var loadedAssembly = Assembly.Load(File.ContentsAsBytes); the File.ContentAsBytes returns the dll as a byte[], via the following System.IO.File.ReadAllBytes("dll location"); The issue is the loaded…
dbones
  • 4,415
  • 3
  • 36
  • 52
14
votes
5 answers

Using Reflection.Emit to create a class implementing an interface

I need to generate a class using Reflection.Emit that implements the following interface. public interface IObject { T Get(string propertyName); } Does anyone have an example of how I would emit the following as a simple test case? class…
karma
  • 141
  • 1
  • 1
  • 3