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

How to dynamically call a class' method in .NET?

How to pass a class and a method name as strings and invoke that class' method? Like void caller(string myclass, string mymethod){ // call myclass.mymethod(); } Thanks
pistacchio
  • 56,889
  • 107
  • 278
  • 420
14
votes
5 answers

Assembly.GetType is returning null

I am trying to dynamically load an encryption assembly but my GetType is returning null, even though I am using the correct class name. Here's the code: //Load encryption assembly. Assembly encryptionAssembly =…
Ste
  • 1,136
  • 2
  • 10
  • 30
14
votes
10 answers

Can I override a private method in Java?

I know I can use reflection to invoke a private method, and to get or set the value of a private variable, but I want to override a method. public class SuperClass { public void printInt() { System.out.println("I am " + getClass() + ".…
numbers longer
  • 331
  • 1
  • 3
  • 9
14
votes
4 answers

What is the VB equivalent of Java's instanceof and isInstance()?

In the spirit of the c# question.. What is the equivalent statements to compare class types in VB.NET?
brasskazoo
  • 76,030
  • 23
  • 64
  • 76
13
votes
2 answers

Parse to Nullable Enum

I am trying to parse a string back to a nullable property of type MyEnum. public MyEnum? MyEnumProperty { get; set; } I am getting an error on line: Enum result = Enum.Parse(t, "One") as Enum; // Type provided must be an Enum. Parameter name:…
Valamas
  • 24,169
  • 25
  • 107
  • 177
13
votes
5 answers

Can I get the calling instance from within a method via reflection/diagnostics?

Is there a way via System.Reflection, System.Diagnostics or other to get a reference to the actual instance that is calling a static method without passing it in to the method itself? For example, something along these lines class A { public…
Scott Nichols
  • 6,108
  • 4
  • 28
  • 23
13
votes
8 answers

Reflection For Static Class in C#

I have created a Static Class and used that in Reflection. But when i accessed the Methods of that class, its showing 5 methods but i have created only 1. The extra methods are Write ToString Equals GetHashCode GetType But i have created only the…
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154
13
votes
5 answers

Detect if the type of an object is a type defined by .NET Framework

How can I determine by reflection if the type of an object is defined by a class in my own assembly or by the .NET Framework? I dont want to supply the name of my own assembly in code, because it should work with any assembly and namespace.
Mathias F
  • 15,906
  • 22
  • 89
  • 159
13
votes
5 answers

Groovy, get enclosing function's name?

I'm using Groovy 1.8.4, trying to get the name of the enclosing function... def myFunction() { println functionName?? } I've tried delegate, this, owner, Groovy complains no such objects found. I also tried the Java hack new…
raffian
  • 31,267
  • 26
  • 103
  • 174
13
votes
3 answers

PropertyInfo.GetValue() - how do you index into a generic parameter using reflection in C#?

This (shortened) code.. for (int i = 0; i < count; i++) { object obj = propertyInfo.GetValue(Tcurrent, new object[] { i }); } .. is throwing a 'TargetParameterCountException : Parameter count mismatch' exception. The underlying type of…
flesh
  • 23,725
  • 24
  • 80
  • 97
13
votes
4 answers

How to get the items count from an IList<> got as an object?

In a method, I get an object. In some situation, this object can be an IList of "something" (I have no control over this "something"). I am trying to: Identify that this object is an IList (of something) Cast the object into an "IList"…
remio
  • 1,242
  • 2
  • 15
  • 36
13
votes
7 answers

Is Reflection really slow?

This is a common belief that reflection is slow and try to avoid it as much as possible. But is that belief true, in the current situation? There has been lot of changes in the current .net versions like, use of IL Weaving (i.e. IL Emit) etc, as…
Bhaskar
  • 10,537
  • 6
  • 53
  • 64
13
votes
2 answers

Command completion in mathematica : suggest rules/options

In current version of Mathematica these keyboard shortcuts are quite handy Ctrl+K completes current command GraphPl -> press Ctrl+K -> GraphPlot Ctrl+Shift+K completes current command and adds argument placeholders which could be replaced with…
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
13
votes
2 answers

Strange Effect with Overridden Properties and Reflection

I've come across a strange behaviour in .NET/Reflection and cannot find any solution/explanation for this: class A { public virtual string TestString { get; set; } } class B : A { public override string TestString { get { return…
naacal
  • 620
  • 1
  • 6
  • 16
13
votes
0 answers

Two parameters causes 'Method in Type does not have an implementation' Exception?

I have an solution with a number of projects. Relevant for the question is an API class library, a CustomTriggers class library and a web site. Both CustomTriggers and web site references API. CustomTriggers implements Interface ITrigger located in…
volapture
  • 131
  • 1
  • 1
  • 4