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

How to execute a private static method with optional parameters via reflection?

I have a class with a private static method with an optional parameter. How do I invoke it from another class via Reflection? There is a similar question, but it does not address static method or optional parameters. public class Foo { private…
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
13
votes
3 answers

Is it possible to get all the eigenclasses in Ruby?

Getting a list of all modules is easy in Ruby: ObjectSpace.each_object(Module).to_a However, is it possible to get a list of all eigenclasses (also known as singleton classes or metaclasses)? Or are eigenclasses invisible? I tried str =…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
13
votes
7 answers

Find only non-inherited interfaces?

I am trying to perform a query on the interfaces of a class via reflection, however the method Type.GetInterfaces() returns all the inherited interfaces also. etc public class Test : ITest { } public interface ITest : ITesting { } The code…
Alex Hope O'Connor
  • 9,354
  • 22
  • 69
  • 112
13
votes
3 answers

Pattern matching on Class[_] type?

I'm trying to use Scala pattern matching on Java Class[_] (in context of using Java reflection from Scala) but I'm getting some unexpected error. The following gives "unreachable code" on the line with case jLong def foo[T](paramType: Class[_]):…
alphageek
  • 770
  • 4
  • 15
13
votes
6 answers

Java how can I with reflection check if a field is initialized or is default value?

So, should be straight forward question. Lets say I have a class with a lot of fields like: String thizz; long that; boolean bar; How can I, with reflection, see if the fields thizz, that and bar have been initialized or left to their default…
rapadura
  • 5,242
  • 7
  • 39
  • 57
13
votes
3 answers

How can I create a dynamic proxy in java that retains parameter annotations on methods?

I currently am trying to proxy some existing JAX/RS resources, in order to allow me to use the Hibernate Validator's method validation support. However, when I proxy my class (currently using cglib 2.2), the FormParam annotation is not present on…
Peter Hart
  • 4,955
  • 2
  • 25
  • 30
13
votes
2 answers

Boxed Value Type comparisons

What i'm trying to achieve here is a straight value comparison of boxed primitive types. ((object)12).Equals((object)12); // Type match will result in a value comparison, ((object)12).Equals((object)12d); // but a type mismatch will not.…
johnDisplayClass
  • 269
  • 3
  • 11
13
votes
3 answers

Calling a function using reflection that has a "params" parameter (MethodBase)

I have MethodBases for two functions: public static int Add(params int[] parameters) { /* ... */ } public static int Add(int a, int b) { /* ... */ } I have a function that calls the MethodBases via a class I made: MethodBase Method; object…
Blam
  • 2,888
  • 3
  • 25
  • 39
13
votes
2 answers

How do I create an instance from a string in C#?

I'm reading information from an XML which contains the type of an object that I need to instantiate along with it's constructor parameters. The object type is actually in another project, within a sibling namespace. (I need to create a…
Ben S
  • 68,394
  • 30
  • 171
  • 212
13
votes
3 answers

get fields with reflection

I want to get all fields that have null values but i aint even getting any fields: [Serializable()] public class BaseClass { [OnDeserialized()] internal void OnDeserializedMethod(StreamingContext context) { …
syncis
  • 1,395
  • 4
  • 25
  • 43
13
votes
3 answers

Java: InvocationTargetException

I am dynamically creating classes in Java and trying to invoke methods in them, however, sometimes I get a java.lang.reflect.InvocationTargetException. PageGenerator1.java (dynamically created) import java.io.PrintStream; import…
Amir Rachum
  • 76,817
  • 74
  • 166
  • 248
13
votes
5 answers

Cast Boxed Object back to Original Type

I expect there's one of two answers to this, either impossible or extremely simple and I've overlooked the obvious Google query. The underlying issue is that I have a generic object being passed in via an EventHandler that boxes the object and…
falquan
  • 597
  • 4
  • 6
  • 11
13
votes
4 answers

Class.forName() equivalent in .NET?

What is the C# way for dynamically getting the type of object and then creating new instances of it? E.g. how do I accomplish the result of following Java code, but in C#: MyClass x = (MyClass) Class.forName("classes.MyChildClass").newInstance();
mihsathe
  • 8,904
  • 12
  • 38
  • 54
13
votes
4 answers

Multiple instances of static variables

I'm experimenting with using different classloaders to load a particular class, and see if the static variables in that class can have different instances. Basically, I'm trying to write code for what Stephen C has mentioned in this answer. Here…
AbdullahC
  • 6,649
  • 3
  • 27
  • 43
13
votes
8 answers

How to remove a property from class at run time

Is it possible to remove a property from class at runtime, like: public Class A { public int num1 {get;set;} public int num2 {get;set;} public int num3 {get;set;} } Class A Obj = new A(); At run time I want to remove num2 from obj. Is it…
KhanZeeshan
  • 1,410
  • 5
  • 23
  • 36