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
386
votes
11 answers

Casting a variable using a Type variable

In C# can I cast a variable of type object to a variable of type T where T is defined in a Type variable?
theringostarrs
  • 11,940
  • 14
  • 50
  • 63
381
votes
10 answers

Set object property using reflection

Is there a way in C# where I can use reflection to set an object property? Ex: MyObject obj = new MyObject(); obj.Name = "Value"; I want to set obj.Name with reflection. Something like: Reflection.SetProperty(obj, "Name") = "Value"; Is there a way…
Melursus
  • 10,328
  • 19
  • 69
  • 103
381
votes
11 answers

How do I use reflection to invoke a private method?

There are a group of private methods in my class, and I need to call one dynamically based on an input value. Both the invoking code and the target methods are in the same instance. The code looks like this: MethodInfo dynMethod =…
Jeromy Irvine
  • 11,614
  • 3
  • 39
  • 52
375
votes
10 answers

Creating an instance using the class name and calling constructor

Is there a way to create an instance of a particular class given the class name (dynamic) and pass parameters to its constructor. Something like: Object object = createInstance("mypackage.MyClass","MyAttributeValue"); Where "MyAttributeValue" is an…
TheLameProgrammer
  • 4,037
  • 5
  • 19
  • 16
370
votes
15 answers

What could cause java.lang.reflect.InvocationTargetException?

Well, I've tried to understand and read what could cause it but I just can't get it: I have this somewhere in my code: try{ .. m.invoke(testObject); .. } catch(AssertionError e){ ... } catch(Exception e){ .. } Thing is that, when it tries…
user550413
  • 4,609
  • 4
  • 25
  • 26
369
votes
15 answers

Why does C++ not have reflection?

This is a somewhat bizarre question. My objectives are to understand the language design decision and to identify the possibilities of reflection in C++. Why C++ language committee did not go towards implementing reflection in the language? Is…
amit kumar
  • 20,438
  • 23
  • 90
  • 126
358
votes
12 answers

Setting a property by reflection with a string value

I'd like to set a property of an object through Reflection, with a value of type string. So, for instance, suppose I have a Ship class, with a property of Latitude, which is a double. Here's what I'd like to do: Ship ship = new Ship(); string value…
David Hodgson
  • 10,104
  • 17
  • 56
  • 77
351
votes
16 answers

Check if a class is derived from a generic class

I have a generic class in my project with derived classes. public class GenericClass : GenericInterface { } public class Test : GenericClass { } Is there any way to find out if a Type object is derived from…
bernhardrusch
  • 11,670
  • 12
  • 48
  • 59
349
votes
7 answers

Convert.ChangeType() fails on Nullable Types

I want to convert a string to an object property value, whose name I have as a string. I am trying to do this like so: string modelProperty = "Some Property Name"; string value = "SomeValue"; var property =…
iboeno
  • 3,809
  • 4
  • 20
  • 13
348
votes
34 answers

How to get function parameter names/values dynamically?

Is there a way to get the function parameter names of a function dynamically? Let’s say my function looks like this: function doSomething(param1, param2, .... paramN){ // fill an array with the parameter name and value // some other code…
vikasde
  • 5,681
  • 10
  • 45
  • 62
343
votes
23 answers

How to tell if a JavaScript function is defined

How do you tell if a function in JavaScript is defined? I want to do something like this function something_cool(text, callback) { alert(text); if( callback != null ) callback(); } But it gets me a callback is not a function error when…
Aaron Lee
  • 5,287
  • 5
  • 24
  • 19
340
votes
16 answers

Reflection - get attribute name and value on property

I have a class, lets call it Book with a property called Name. With that property, I have an attribute associated with it. public class Book { [Author("AuthorName")] public string Name { get; private set; } } In my main…
user619891
323
votes
28 answers

How can I add reflection to a C++ application?

I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some limited information using RTTI. Which additional…
Nick
  • 27,566
  • 12
  • 60
  • 72
309
votes
11 answers

Getting all types in a namespace via reflection

How do you get all the classes in a namespace through reflection in C#?
Chethan
276
votes
14 answers

How to determine if a type implements a specific generic interface type

Assume the following type definitions: public interface IFoo : IBar {} public class Foo : IFoo {} How do I find out whether the type Foo implements the generic interface IBar when only the mangled type is available?
sduplooy
  • 14,340
  • 9
  • 41
  • 60