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

Can I override a hidden (but public) method and call its super method?

There is a non public api that I need to override in order to workaround a quirk with Android's WebView. The api is hidden but it is public: /** * ... * * @hide pending API council approval */ public boolean selectText() { ... } So I can…
cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171
12
votes
3 answers

Get type using reflection

I am trying get type of property of my class by using of reflection but its returning my only RuntimePropertyInfo - as a name of a type. I have object MyObject actualData - it contains property - "name" as string and "Item" as my type DatumType When…
Martin Ch
  • 1,337
  • 4
  • 21
  • 42
12
votes
3 answers

Why GetCustomAttributes returns object[] instead of Attribute[]?

Just curious, see MemberInfo.GetCustomAttributes. Is it hinting that it may contain a non-Attribute object?
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
12
votes
4 answers

Objective-C Reflection for generic NSCoding implementation

Is there any means of reflection in Objective-C that would allow you to write generic NSCoding implementations by inspecting the public properties of an object and generating generic implementations of encodeWithCoder: and initWithCoder: . I'm…
Dougnukem
  • 14,709
  • 24
  • 89
  • 130
12
votes
6 answers

How do I build a Java type object at runtime from a generic type definition and runtime type parameters?

Assuming a generic type declaration (Java) class Foo { public T bar; } how can I, at runtime, instantiate a Type object that represents Foo parameterized over a specific type T (also known only at runtime)?
SoftMemes
  • 5,602
  • 4
  • 32
  • 61
12
votes
4 answers

Is there a way to find if a Field is boolean the same as isPrimitive()?

Is there a way to find if a Field is boolean in Java reflection the same as isPrimitive()? Field fieldlist[] = clazz.getDeclaredFields(); for (int i = 0; fieldlist.length & gt; i; i++) { Field fld = fieldlist[i]; if (fld.getClass().isPrimitive())…
nabil
  • 904
  • 3
  • 12
  • 28
12
votes
5 answers

Does Type.GUID uniquely identifies each type across compilations?

Possible Duplicate: Are automatically generated GUIDs for types in .NET consistent? I want to use Type as a key dictionary, but I'd rather use either full type name or Type.GUID. How reliable and correct is Type.GUID for this task? Ayende Rahien…
Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
12
votes
2 answers

dapper PropInfo Setter for inherited EntitySet from abstract class reference is null

I am trying to replace a nasty LINQ 2 SQL hit with some dapper queries to improve performanace. In doing so I have to weave a bunch of different objects together in order to create the big object required to hold all the information I need for ASN…
John
  • 392
  • 3
  • 17
12
votes
4 answers

How to verify whether a type overloads/supports a certain operator?

How can I check whether a certain type implements a certain operator? struct CustomOperatorsClass { public int Value { get; private set; } public CustomOperatorsClass( int value ) : this() { Value = value; } …
Steven Jeuris
  • 18,274
  • 9
  • 70
  • 161
12
votes
8 answers

Reflection in C++

I've been working for years with Java. During those years, I've made extensive (or maybe just frequent) use of reflection, and found it useful and enjoyable. But 8 months ago I changed my job, and now Java is just a memory, and I'm getting my hands…
aitor
  • 123
  • 2
  • 7
12
votes
1 answer

Creating F# record through reflection

How can I create a record type in F# by using reflection? Thanks
Giuseppe Maggiore
  • 2,011
  • 1
  • 23
  • 31
12
votes
1 answer

Using getActualTypeArguments in a generic context

There are other related questions e.g. 6624113, 3403909, 4516891 but my question is simpler and more specific. I want to know at runtime what type my class was parameterized with - I want a Class object of the type of the type parameter. Because of…
Andy Balaam
  • 6,423
  • 6
  • 34
  • 37
12
votes
2 answers

How do you pass parameters by ref when calling a static method using reflection?

I'm calling a static method on an object using reflection: MyType.GetMethod("MyMethod", BindingFlags.Static).Invoke(null, new object[] { Parameter1, Parameter2 }); How do you pass parameters by ref, rather that by value? I assume they would be by…
Deane
  • 8,269
  • 12
  • 58
  • 108
12
votes
1 answer

Reflection methods are not working when used proguard for android application

I am facing a problem when i use proguard for application having used telephonyservice apis using reflection in android. I have defined a package com.android.internal.telephony and there i have copied ITelephony.aidl file. Here is the snippet of…
nawab
  • 362
  • 4
  • 9
12
votes
2 answers

get methods of an object in ruby

I'm a little confused about this behavior from the ruby (1.9) interpreter class Foo def pub private_thing end private def private_thing puts "private touch" end end x = Foo.new x.pub private touch => nil so far so…
Ramy
  • 20,541
  • 41
  • 103
  • 153