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
14
votes
6 answers

How do I determine if System.Type is a custom type or a Framework type?

I want to distinctly determine if the type that I have is of custom class type (MyClass) or one provided by the Framework (System.String). Is there any way in reflection that I can distinguish my class type from system.string or other Framework…
Nilotpal Das
  • 585
  • 1
  • 6
  • 15
14
votes
2 answers

Using Reflection to set a Property with a type of List

How can I use reflection to create a generic List with a custom class (List)? I need to be able to add values and use propertyInfo.SetValue(..., ..., ...) to store it. Would I be better off storing these List<>'s as some other data…
dragonjujo
  • 358
  • 1
  • 2
  • 10
14
votes
1 answer

Scala: Accessing package visible methods through structural types outside the package

This does not work as expected (since I am trying to call a package private run from outside Services): object Services { class HelloPrinter { private[Services] def run = "Hello" } } val obj = new Services.HelloPrinter But, surprisingly…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
14
votes
2 answers

How can I find all implementations of interface in classpath?

I'm implementing an interface and now I'd like to get all implementations of this interface in classpath. Is this possible or should I do something else?
newbie
  • 24,286
  • 80
  • 201
  • 301
14
votes
4 answers

How to cast DbSet to List

Given the following simplified Entity Framework 6 context, I am trying to populate a List with the entities but having problems with how to cast (I believe) via reflection. public class FooContext : DbContext { public virtual IDbSet Foo…
Kyle
  • 951
  • 3
  • 14
  • 31
14
votes
3 answers

Reflection and Operator Overloads in C#

Here's the deal. I've got a program that will load a given assembly, parse through all Types and their Members and compile a TreeView (very similar to old MSDN site) and then build HTML pages for each node in the TreeView. It basically takes a…
Mike U
  • 709
  • 6
  • 11
14
votes
3 answers

Get PropertyInfo from property instead of name

Say, for example, I've got this simple class: public class MyClass { public String MyProperty { get; set; } } The way to get the PropertyInfo for MyProperty would be: typeof(MyClass).GetProperty("MyProperty"); This sucks! Why? Easy: it will…
Sam
  • 28,421
  • 49
  • 167
  • 247
14
votes
3 answers

Deep recursive objects comparison (once again)

There are two similar questions on SO: Is there a Java utility to do a deep comparison of two objects? Deep reflective compare equals but, it is funny, none of them gives a fully correct answer to the question. What I and other questions' authors…
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
14
votes
6 answers

How to make Spring accept fluent (non-void) setters?

I have an API which I am turning into an internal DSL. As such, most methods in my PoJos return a reference to this so that I can chain methods together declaratively as such (syntactic sugar). myComponent .setID("MyId") .setProperty("One") …
Chris
  • 4,450
  • 3
  • 38
  • 49
14
votes
3 answers

How can I check a class has no arguments constructor

Object obj = new Object(); try { obj.getClass().getConstructor(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { dosomething(); e.printStackTrace(); …
Q.yuan
  • 151
  • 1
  • 5
14
votes
8 answers

How can I map a String to a function in Java?

Currently, I have a bunch of Java classes that implement a Processor interface, meaning they all have a processRequest(String key) method. The idea is that each class has a few (say, <10) member Strings, and each of those maps to a method in that…
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
14
votes
4 answers

How do I add attributes to a method at runtime?

We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this: [EventPublication("example",…
Simon
  • 25,468
  • 44
  • 152
  • 266
14
votes
5 answers

Comparing Object properties using reflection

I have two classes Address and Employee as follows: public class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } public string City { get; set; } public string State { get; set; } …
Kumar
  • 2,863
  • 11
  • 45
  • 60
14
votes
5 answers

CreateDelegate with unknown types

I am trying to create Delegate for reading/writing properties of unknown type of class at runtime. I have a generic class Main and a method which looks like this: Delegate.CreateDelegate(typeof(Func), get) where get is a MethodInfo of…
Giorgi
  • 30,270
  • 13
  • 89
  • 125
14
votes
5 answers

Assembly.CreateInstance to resolve IoC Container

I am trying to create an instance of a class (at runtime via a string) using the following code: Assembly assembly = Assembly.GetAssembly(typeAssembly); object instance = assembly.CreateInstance(typeName); //throws MissingMethodException Type…
user3117252
  • 389
  • 1
  • 5
  • 11