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

How do I compare two PropertyInfos or methods reliably?

Same for methods too: I am given two instances of PropertyInfo or methods which have been extracted from the class they sit on via GetProperty() or GetMember() etc, (or from a MemberExpression maybe). I want to determine if they are in fact…
metdos
  • 13,411
  • 17
  • 77
  • 120
12
votes
1 answer

How to get Column names of JPA entity

All my JPA entity classes implement an interface called Entity which is defined like this: public interface Entity extends Serializable { // some methods } Some of the fields of my JPA entity have @Column annotation on top of them and some don't.…
Aman
  • 425
  • 1
  • 5
  • 22
12
votes
5 answers

How to generate method signature?

Desired output examples: (Lorg/w3c/dom/Node;)Lorg/w3c/dom/Node; (Ljava/lang/String;)Lorg/w3c/dom/Attr; Such signatures can be generated using javap utility: javap -s -p org.w3c.dom.Node But is there any way to generate them programmatically. I…
alex2k8
  • 42,496
  • 57
  • 170
  • 221
12
votes
1 answer

How does ComponentScan work?

@ComponentScan will give you a list of all the classes with the @Component annotation in a package (or @Service/@Repository). To do this I imagine they use reflection to enumerate all the classes in a package and find the ones with that…
David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
12
votes
3 answers

How to unwrap the original object from a dynamic proxy

what's the best approach to unwrap a dynamic proxy to retrieve the original object beneath? The dynamic proxy has been created using java.lang.reflect.Proxy.newProxyInstance() Thank you.
MRalwasser
  • 15,605
  • 15
  • 101
  • 147
12
votes
4 answers

Activator.CreateInstance with private sealed class

I'm trying to new up a LocalCommand instance which is a private class of System.Data.SqlClient.SqlCommandSet. I seem to be able to grab the type information just fine: Assembly sysData = Assembly.Load("System.Data, Version=2.0.0.0, Culture=neutral,…
Sneal
12
votes
6 answers

Java equivalent of python's getattr?

I'm converting some python code to java, and have a situation where I need to call methods of an object but don't know which methods until runtime. In python I resolve this by using getattr on my object and passing it a string that is the name of my…
Johnny
  • 121
  • 1
  • 3
12
votes
4 answers

Keep NULL rows last on Dynamic Linq Order By

I am using this snippet below for Ordering my Linq queries dynamically and works great. I am not great at reflection or complex linq queries but I need a way that when ascending order is used, that NULL values are last and vice versa. So if my…
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
12
votes
2 answers

Obtaining the full info about fields of the case class in Scala

Consider the following class and method: case class User(id: Long, name: String) { private var foo = "Foo" // shouldn't be printed val bar = "bar" // also shouldn't be printed } case class Message(id: Long, userId: Long, text: String) def…
alex
  • 942
  • 1
  • 10
  • 26
12
votes
2 answers

How to specify Namespace for a type created through Reflection.Emit?

Apologies if I'm missing something obvious, but when I create a new type with Reflection.Emit, how do I specify what namespace it should be in? ie.. AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "TestDynamic"; AssemblyBuilder…
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
12
votes
1 answer

Enum's values() method access level

JLS 8 states that each enum class has implicitly declared method: public static E[] values(); So, it is public by specification. At the same time Class.getEnumConstantsShared() method forcibly makes it accessible: final Method values =…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
12
votes
4 answers

MethodInfo Equality for Declaring Type

I need to check equality between two MethodInfos. They are actually the exact same MethodInfo with the exception of the ReflectedType (that is, the DeclaringType is the same and the methods should actually have the same body). There are a number of…
Jeff
  • 35,755
  • 15
  • 108
  • 220
12
votes
4 answers

Get Assembly version on windows phone 7

In my c# applications I usually get the version (to show the customer) using the following code: System.Reflection.Assembly.GetExecutingAssembly().GetName().Version This does not work in Windows Phone 7 (it hangs the emulator, and phone crashing is…
Sam
  • 28,421
  • 49
  • 167
  • 247
12
votes
2 answers

Reflection - Call constructor with parameters

I read type from loaded assemblies for example: var someType = loadedAssemblies .Where(a => a != null && a.FullName.StartsWith("MY.")) .SelectMany(a => a.GetTypes()) .Distinct() .ToArray()[0]; If…
Simon
  • 1,955
  • 5
  • 35
  • 49
12
votes
1 answer

Golang: cast interface back to its original type

I couldn't really find an answer to this, even though I looked up the Go documentation and examples. Is it possible to cast an interface back to its original type, dynamically? I know I can do something like this: var myint int = 5 var myinterface…
Kugel
  • 808
  • 8
  • 26
1 2 3
99
100