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 can I determine the parameters required by an arbitrary piece of T-SQL?

Basically, I'm looking for an equivalent to SqlCommandBuilder.DeriveParameters that will work for arbitrary T-SQL. For example, this query requires one parameter: SELECT @Foo [Foo], '@Bar' [Bar], @Baz [Baz] I basically need to extract: new[] {…
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
13
votes
1 answer

How to get DisplayAttribute of a property by Reflection?

I have a Helper method like this to get me the PropertyName (trying to avoid magic strings) public static string GetPropertyName(Expression> expression) { var body = (MemberExpression) expression.Body; …
Houman
  • 64,245
  • 87
  • 278
  • 460
13
votes
2 answers

How do I check if a type fits the unmanaged constraint in C#?

How do I check if a type T fits the unmanaged type constraint, such that it could be used in a context like this: class Foo where T : unmanaged? My first idea was typeof(T).IsUnmanaged or something similar, but that isn't a property/field of the…
John
  • 598
  • 2
  • 7
  • 22
13
votes
2 answers

Get contained type in a List through reflection?

Through reflection, is there some way for me to look at a generic List's contained type to see what type the collection is of? For example: I have a simple set of business objects that derive from an interface, like this: public interface…
joshua.ewer
  • 3,944
  • 3
  • 25
  • 35
13
votes
1 answer

Call private method retaining call stack

I am trying to find a solution to 'break into non-public methods'. I just want to call RuntimeMethodInfo.InternalGetCurrentMethod(...), passing my own parameter (so I can implement GetCallingMethod()), or directly use…
Edwin
  • 527
  • 7
  • 15
13
votes
2 answers

What does "+" mean in reflected FullName and '*' after Member c#

I'm currently dealing with reflection in c#. After: Assembly.LoadFile(@"C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.7\System.Numerics.Vectors.dll").GetTypes() And i found this: [System.Numerics.Matrix4x4],…
Kacper
  • 451
  • 6
  • 17
13
votes
5 answers

Modify a method using Annotations

How can I change what a method is doing in Java ? I mean, I am trying to use annotations to make the following code @Anno1(Argument = "Option1") public class TestClass { @Anno2 public void test() { } } Into public class…
user418748
13
votes
2 answers

Is there a Swift equivalent of C#'s 'nameof()' function to get a variable or member's name at compile time?

Ok, there's an existing question here on S/O with the following title: Swift: Get Variable Actual Name as String By it's name, it seems that's exactly what I want. However, looking at the accepted answer (and the other non-accepted ones), they are…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
13
votes
2 answers

How do I use Kotlin object by reflection

Let's say I need to set property A given by a String in Kotlin object O given by a String by reflection. If O was a class I could do something like this (disregard it has no sense): fun setValue(ownerClassName: String, fieldName: String, value :…
ssuukk
  • 8,200
  • 7
  • 35
  • 47
13
votes
1 answer

Kotlin get Field Annotation always empty

I have the following Kotlin Annotation @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY_GETTER) @Retention(AnnotationRetention.RUNTIME) annotation class Field(val value: String) And the following Test Code class…
Grisu118
  • 339
  • 1
  • 3
  • 7
13
votes
4 answers

How to access generic property without knowing the closed generic type

I have a generic Type as follows public class TestGeneric { public T Data { get; set; } public TestGeneric(T data) { this.Data = data; } } If i have now an object (which is coming from some external source) from which i…
Martin Booka Weser
  • 3,192
  • 5
  • 28
  • 41
13
votes
1 answer

Is there a good reflection library available for Scala?

I'm working on a library that needs reflection, and needs Scala-specific information as opposed to what is available via the standard Java reflection API. Right now I'm using the undocumented code in scalap (the Scala equivalent to javap) and…
Erik Engbrecht
  • 3,174
  • 17
  • 23
13
votes
4 answers

SQLAlchemy declarative syntax with autoload (reflection) in Pylons

I would like to use autoload to use an existings database. I know how to do it without declarative syntax (model/_init_.py): def init_model(engine): """Call me before using any of the tables or classes in the model""" t_events =…
Juliusz Gonera
  • 4,658
  • 5
  • 32
  • 35
13
votes
2 answers

Retrieve the names of all the boolean properties of a class which are true

I have a class that has lots of bool properties. How can I create another property that is a list of strings that contains the name of the properties which have a value of true? See initial attempt below - can't quite figure out how to filter the…
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
13
votes
3 answers

How to iterate on all properties of an object in C#?

I am new to C#, I want to write a function to iterate over properties of an object and set all null strings to "". I have heard that it is possible using something called "Reflection" but I don't know how. Thanks
Ristovak
  • 491
  • 1
  • 5
  • 10