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
1 answer

"Object does not match target type" when calling methods using string in C#

I'm trying to call a method using a string, but there a problem: void make_moviment(string mov,Vector3 new_mov){ GameObject past_panel = GameObject.Find(actual_level.ToString()); Type t =…
Bernardo Bacalhau
  • 155
  • 1
  • 1
  • 8
13
votes
4 answers

Get the paths of all referenced assemblies

How do I get the paths of all the assemblies referenced by the currently executing assembly? GetReferencedAssmblies() gives me the AssemblyName[]s. How do I get to where they are loaded from, from there?
Water Cooler v2
  • 32,724
  • 54
  • 166
  • 336
13
votes
1 answer

Invoking a method of a Generic Class

Here is the Context : I try to code a mapper for converting my DomainModel Objects to ViewModel Ojects dynamically. The problem I get, it's when I try to invoke a method of generic class by reflection I get this error…
dervlap
  • 406
  • 4
  • 14
13
votes
3 answers

How can I programmatically change the argspec of a function in a python decorator?

Given a function: def func(f1, kw='default'): pass bare_argspec = inspect.getargspec(func) @decorator def func2(f1, kw='default'): pass decorated_argspec = inspect.getargspec(func2) How can I create a decorator such that bare_argspec ==…
Chris R
  • 17,546
  • 23
  • 105
  • 172
13
votes
2 answers

Accessing static variables of a model class using reflection

I have a model class that contains some static variables and properties. In Runtime I can get the properties; let instance = entity.init() let mirror = Mirror(reflecting:instance) var propertyStrings = [String]() for (propertyName, childMirror)…
13
votes
4 answers

How can I discover classes in a specific package in python?

I have a package of plug-in style modules. It looks like this: /Plugins /Plugins/__init__.py /Plugins/Plugin1.py /Plugins/Plugin2.py etc... Each .py file contains a class that derives from PluginBaseClass. So I need to list every module in the…
Jason Webb
  • 7,938
  • 9
  • 40
  • 49
13
votes
4 answers

Await the result of Task using reflection in a non-generic method

Consider the following case: class A { public int Id; } class B : A { } class Main { public async Task Create(Type type) { MethodInfo method = this.GetType().GetMethod("Create", new Type[] { typeof(string)…
Razzie
  • 30,834
  • 11
  • 63
  • 78
13
votes
5 answers

(Why) Is Reflection so expensive in .Net?

Possible Duplicate: What is the “cost” of reflection? Does anyone have a good explanation of the generally accepted mantra that reflection == bad performance? For example, how expensive is it to iterate over a type's properties collection and…
Manu
  • 28,753
  • 28
  • 75
  • 83
13
votes
1 answer

Create Expression from PropertyInfo

I'm using an API that expects an Expression>, and uses this to create mappings between different objects: Map(x => x.Id).To("Id__c"); // The expression is "x => x.Id" How can I create the necessary expression from a PropertyInfo?…
Didaxis
  • 8,486
  • 7
  • 52
  • 89
13
votes
2 answers

Get rawValue from enum in a generic function

Update 8/28/2015: This will be solved in Swift 2 See Twitter response from Swift compiler developer Update 10/23/2015: With Swift 2 generics you still cannot get the rawValue. You do can get the associated value. Original question: I have some…
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
13
votes
2 answers

PropertyInfo SetValue and nulls

If I have something like: object value = null; Foo foo = new Foo(); PropertyInfo property = Foo.GetProperties().Single(p => p.Name == "IntProperty"); property.SetValue(foo, value, null); Then foo.IntProperty gets set to 0, even though value =…
Nelson Rothermel
  • 9,436
  • 8
  • 62
  • 81
13
votes
1 answer

Reflection: How to get the underlying type of a by-ref type

I was surprised to learn that "ref" and "out" parameters are not marked by a special attribute, despite the existence of ParameterInfo.IsOut, ParameterInfo.IsIn (both of which are always false as far as I can see), ParameterAttributes.In and…
Qwertie
  • 16,354
  • 20
  • 105
  • 148
13
votes
2 answers

How to prevent access via reflection?

In Java docs it mentioned that using f.setAccessible(true) method we can violate the principal of encapsulation. But if I am writing any class which has full security, for instance with a private variable, how can I prevent it from being accessed…
Naz
  • 390
  • 1
  • 4
  • 15
13
votes
1 answer

Get pointer to a struct field value

I'm trying to make a function that converts a struct in the way mysql rows.Scan function needs it, so I don't need to pass manually lots of parameters. Note: I know the existence of sqlx and the alternative of writing manually in separate lines…
alfonsodev
  • 2,714
  • 2
  • 23
  • 30
13
votes
4 answers

Any way to access the type of a Scala Option declaration at runtime using reflection?

So, I have a Scala class that looks like this: class TestClass { var value: Option[Int] = None } and I'm tackling a problem where I have a String value and I want to coerce it into that Option[Int] at runtime using reflection. So, in another…
Graham Lea
  • 5,797
  • 3
  • 40
  • 55