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
12
votes
2 answers

How to change a kotlin private val using reflection?

I can access a private val value using reflection as below fun main() { val mainClass = MainClass() val f = MainClass::class.memberProperties.find { it.name == "info" } f?.let { it.isAccessible = true val w =…
Elye
  • 53,639
  • 54
  • 212
  • 474
12
votes
3 answers

Is there a way to get all namespaces you're 'using' within a class through C# code?

Is there any way to get a List which contains all 'usings' within a namespace/class? For instance using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using…
Conrad Clark
  • 4,533
  • 5
  • 45
  • 70
12
votes
1 answer

Java - Relocate references at runtime

So first off I know that you can relocate all references your compiled jar with various shadow plugins on different build systems. I know how that works and am already using it. However I ran into a problem where I can't do that at compile…
BrainStone
  • 3,028
  • 6
  • 32
  • 59
12
votes
6 answers

How to get default constructor when parameters are optional

I'm using Type.GetConstructor(Type.EmptyTypes) to get the default constructor for a class. It works if the class has a default constructor with no parameters (class A). But it doesn't work if a class has a constructor with all parameters optional…
dlsou
  • 635
  • 2
  • 8
  • 18
12
votes
2 answers

Find out if type is instantiable

In C#, how can I find out if a Type can be instantiated? I am trying to avoid an Activator.CreateInstance exception. My current method is type.IsClass && !type.IsInterface, but I am worried this could fail on abstract classes, etc. I also…
user664939
  • 1,977
  • 2
  • 20
  • 35
12
votes
3 answers

Java reflection: getMethod(String method, Object[].class) not working

I have the following code: public void myMethod(Object... args) { System.out.println("this is myMethod"); } public void invokeMyMethod() { Method s = this.getClass().getMethod("myMethod", Object[].class); Object[] ex = new Object[2]; …
myuser7k23
  • 327
  • 2
  • 3
  • 9
12
votes
1 answer

Lambda stack trace missing when using NativeMethodAccessor instead of GeneratedMethodAccessor

A couple days ago, I got a support ticket for this NullPointerException: com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract com.redacted.SalesResponsePagination…
walen
  • 7,103
  • 2
  • 37
  • 58
12
votes
3 answers

C# Get type of fixed field in unsafe struct with reflection

I'm trying to get the field types of an unsafe struct using some fixed fields. The fixed fields FieldType do not return the actual type. [StructLayout(LayoutKind.Sequential, Pack = 1)] public unsafe struct MyStruct { public UInt32 Field1; …
Joe
  • 964
  • 1
  • 10
  • 27
12
votes
4 answers

How to get the initialisation value for a Java Class reference

I have a Class reference for an arbitrary type. How to get that type's initialisation value? Is there some library method for this or do I have to roll my own, such as: Class klass = ... Object init = (klass == boolean.class) ? false …
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
12
votes
3 answers

How to Apply XmlIncludeAttribute to TypeBuilder?

I am developing a library in C# that generates runtime types using System.Reflection.Emit.TypeBuilder class and i want to generate the following class hierarchy: [XmlInclude(typeof(Derived))] public class Base { } public class Derived : Base { } I…
Timothy Ghanem
  • 1,606
  • 11
  • 20
12
votes
1 answer

How to instantiate class by it's string name in Python from CURRENT file?

Suppose I have myfile.py with some classes A, B and C defined INSIDE it. Now I want to instantiate class by it's name in str. I don't understand what to pass to getattr in order to do this. All examples like this assume that classes are in other…
Dims
  • 47,675
  • 117
  • 331
  • 600
12
votes
2 answers

Java: static nested classes and reflection: "$" vs "."

If I have a class com.example.test.Enum2.Test as in the code below, why does getCanonicalName() return com.example.test.Enum2.Test but Class.forName() requires "com.example.test.Enum2$Test" as an argument? Is there a way to be consistent, so that I…
Jason S
  • 184,598
  • 164
  • 608
  • 970
12
votes
2 answers

MethodBase.GetCurrentMethod() Performance?

I have written a log class and a function as in the following code: Log(System.Reflection.MethodBase methodBase, string message) Every time I log something I also log the class name from the methodBase.Name and methodBase.DeclaringType.Name. I read…
Ioannis
  • 2,985
  • 4
  • 25
  • 19
12
votes
2 answers

Using reflection read properties of an object containing array of another object

How can I read the properties of an object that contains an element of array type using reflection in c#. If I have a method called GetMyProperties and I determine that the object is a custom type then how can I read the properties of an array and…
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112
12
votes
2 answers

Reflection says that interface method are virtual in the implemented type, when they aren't?

I have the following code in an unit test public bool TestMethodsOf() { var impl = typeof(T); var valid = true; foreach (var iface in impl.GetInterfaces().Where(i => typeof(I).IsAssignableFrom(i))) { var members =…
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
1 2 3
99
100