Skip to main content

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.

Filter by
Sorted by
Tagged with
2551 votes
24 answers
1.0m views

What is reflection and why is it useful?

What is reflection, and why is it useful? I'm particularly interested in Java, but I assume the principles are the same in any language.
Lehane's user avatar
  • 48.2k
2446 votes
18 answers
1.2m views

Calling a function of a module by using its name (a string)

How do I call a function, using a string with the function's name? For example: import foo func_name = "bar" call(foo, func_name) # calls foo.bar()
ricree's user avatar
  • 36.3k
1300 votes
9 answers
365k views

How do I call a generic method using a Type variable?

What's the best way to call a generic method when the type parameter isn't known at compile time, but instead is obtained dynamically at runtime? Consider the following sample code - inside the ...
Bevan's user avatar
  • 44.1k
1244 votes
32 answers
1.0m views

How can I create a generic array in Java?

Due to the implementation of Java generics, you can't have code like this: public class GenSet<E> { private E a[]; public GenSet() { a = new E[INITIAL_ARRAY_LENGTH]; // Error: ...
tatsuhirosatou's user avatar
1196 votes
25 answers
1.1m views

Get property value from string using reflection

I am trying implement the Data transformation using Reflection1 example in my code. The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties ...
pedrofernandes's user avatar
945 votes
31 answers
808k views

How do I get the path of the assembly the code is in?

Is there a way to get the path for the assembly in which the current code resides? I do not want the path of the calling assembly, just the one containing the code. Basically my unit test needs to ...
George Mauer's user avatar
932 votes
11 answers
753k views

How to create a new object instance from a Type

One may not always know the Type of an object at compile-time, but may need to create an instance of the Type. How do you get a new object instance from a Type?
tags2k's user avatar
  • 83.7k
784 votes
11 answers
772k views

How to get the list of properties of a class?

How do I get a list of all the properties of a class?
user avatar
778 votes
23 answers
1.1m views

How do I invoke a Java method when given the method name as a string?

If I have two variables: Object obj; String methodName = "getName"; Without knowing the class of obj, how can I call the method identified by methodName on it? The method being called has no ...
brasskazoo's user avatar
  • 77.9k
723 votes
16 answers
345k views

How to determine if a type implements an interface with C# reflection

Does reflection in C# offer a way to determine if some given System.Type type models some interface? public interface IMyInterface {} public class MyType : IMyInterface {} // should yield 'true' ...
Yippie-Ki-Yay's user avatar
686 votes
11 answers
621k views

C# getting its own class name

If I have a class called MyProgram, is there a way of retrieving "MyProgram" as a string?
deltanovember's user avatar
671 votes
30 answers
869k views

Get generic type of class at runtime

How can I achieve this? public class GenericClass<T> { public Type getMyType() { //How do I return the type of T? } } Everything I have tried so far always returns type ...
Glenn's user avatar
  • 6,735
666 votes
19 answers
399k views

Getting all types that implement an interface

Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations? This is what I want to re-write: foreach (Type t in this....
juan's user avatar
  • 81.3k
652 votes
30 answers
528k views

Can you find all classes in a package using reflection?

Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)
Jonik's user avatar
  • 81.2k
611 votes
15 answers
359k views

Checking if a variable is defined?

How can I check whether a variable is defined in Ruby? Is there an isset-type method available?
readonly's user avatar
  • 351k
607 votes
4 answers
179k views

What are the use(s) for struct tags in Go?

In the Go Language Specification, it mentions a brief overview of tags: A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in ...
liamzebedee's user avatar
  • 14.3k
601 votes
16 answers
184k views

Programmatic equivalent of default(Type)

I'm using reflection to loop through a Type's properties and set certain types to their default. Now, I could do a switch on the type and set the default(Type) explicitly, but I'd rather do it in one ...
tags2k's user avatar
  • 83.7k
594 votes
21 answers
1.1m views

How to list all functions in a module?

I have a Python module installed on my system and I'd like to be able to see what functions/classes/methods are available in it. I want to call the help function on each one. In Ruby I can do ...
Chris Gow's user avatar
  • 7,754
584 votes
27 answers
430k views

Getting attributes of Enum's value

I would like to know if it is possible to get attributes of the enum values and not of the enum itself? For example, suppose I have the following enum: using System.ComponentModel; // for ...
Alex K's user avatar
  • 11.1k
579 votes
26 answers
623k views

How do I get the path and name of the python file that is currently executing?

I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process. For example, let's say I have three files. Using execfile: ...
Ray's user avatar
  • 190k
577 votes
17 answers
380k views

Change private static final field using Java reflection

I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final ...
fixitagain's user avatar
  • 6,673
562 votes
14 answers
379k views

How to read the value of a private field from a different class in Java?

I have a poorly designed class in a 3rd-party JAR and I need to access one of its private fields. For example, why should I need to choose private field is it necessary? class IWasDesignedPoorly { ...
Frank Krueger's user avatar
545 votes
23 answers
487k views

Getting the name of the currently executing method

Is there a way to get the name of the currently executing method in Java?
Omar Kooheji's user avatar
  • 55.3k
509 votes
16 answers
284k views

What is the difference between instanceof and Class.isAssignableFrom(...)?

Which of the following is better? a instanceof B or B.class.isAssignableFrom(a.getClass()) The only difference that I know of is, when 'a' is null, the first returns false, while the second throws ...
Megamug's user avatar
  • 6,307
440 votes
32 answers
325k views

Get class name of object as string in Swift

Getting the classname of an object as String using: object_getClassName(myViewController) returns something like this: _TtC5AppName22CalendarViewController I am looking for the pure version: "...
Bernd's user avatar
  • 11.4k
437 votes
15 answers
299k views

Test if object implements interface

What is the simplest way of testing if an object implements a given interface in C#? (Answer to this question in Java)
JoshRivers's user avatar
  • 10.2k
421 votes
13 answers
322k views

How can I get a list of all classes within current module in Python?

I've seen plenty of examples of people extracting all of the classes from a module, usually something like: # foo.py class Foo: pass # test.py import inspect import foo for name, obj in inspect....
mcccclean's user avatar
  • 7,841
419 votes
10 answers
124k views

Open Source Alternatives to Reflector? [closed]

Just to ask if anyone knows of an open source alternative to RedGate's Reflector? I'm interested in checking out how a tool similar to Reflector actually works. Note, if you know of a free but not ...
Tangiest's user avatar
  • 44.3k
417 votes
16 answers
60k views

Is a Java string really immutable?

We all know that String is immutable in Java, but check the following code: String s1 = "Hello World"; String s2 = "Hello World"; String s3 = s1.substring(6); System.out.println(s1); // Hello ...
Darshan Patel's user avatar
416 votes
5 answers
260k views

How do I check if a type is a subtype OR the type of an object?

To check if a type is a subclass of another type in C#, it's easy: typeof (SubClass).IsSubclassOf(typeof (BaseClass)); // returns true However, this will fail: typeof (BaseClass).IsSubclassOf(...
Daniel T.'s user avatar
  • 38k
394 votes
11 answers
401k views

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's user avatar
385 votes
10 answers
343k views

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....
Melursus's user avatar
  • 10.5k
385 votes
12 answers
229k views

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 ...
Jeromy Irvine's user avatar
381 votes
10 answers
583k views

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","...
TheLameProgrammer's user avatar
380 votes
16 answers
1.4m views

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(...
user550413's user avatar
  • 4,719
372 votes
16 answers
71k views

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 ...
amit kumar's user avatar
  • 20.8k
369 votes
12 answers
354k views

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 ...
David Hodgson's user avatar
357 votes
16 answers
192k views

Check if a class is derived from a generic class

I have a generic class in my project with derived classes. public class GenericClass<T> : GenericInterface<T> { } public class Test : GenericClass<SomeType> { } Is there any way ...
bernhardrusch's user avatar
355 votes
7 answers
96k views

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 ...
iboeno's user avatar
  • 3,869
353 votes
24 answers
220k views

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 ...
Aaron Lee's user avatar
  • 5,407
350 votes
36 answers
278k views

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 ...
vikasde's user avatar
  • 5,711
346 votes
16 answers
467k views

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 { ...
user avatar
332 votes
28 answers
292k views

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 ...
Nick's user avatar
  • 27.9k
317 votes
11 answers
214k views

Getting all types in a namespace via reflection

How do you get all the classes in a namespace through reflection in C#?
user avatar
280 votes
18 answers
238k views

Type.GetType("namespace.a.b.ClassName") returns null

This code: Type.GetType("namespace.a.b.ClassName") returns null. I have in the usings: using namespace.a.b; The type exists, it's in a different class library, and I need to get it by it's ...
Omu's user avatar
  • 70.8k
280 votes
11 answers
229k views

Find a private field with Reflection?

Given this class class Foo { // Want to find _bar with reflection [SomeAttribute] private string _bar; public string BigBar { get { return this._bar; } } } I want to ...
David Basarab's user avatar
279 votes
14 answers
119k views

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

Assume the following type definitions: public interface IFoo<T> : IBar<T> {} public class Foo<T> : IFoo<T> {} How do I find out whether the type Foo implements the generic ...
sduplooy's user avatar
  • 14.6k
272 votes
13 answers
344k views

Get name of property as a string

(See below solution I created using the answer I accepted) I'm trying to improve the maintainability of some code involving reflection. The app has a .NET Remoting interface exposing (among other ...
Jim C's user avatar
  • 4,626
264 votes
3 answers
56k views

BindingFlags.IgnoreCase not working for Type.GetProperty()?

Imagine the following A type T has a field Company. When executing the following method it works perfectly: Type t = typeof(T); t.GetProperty("Company") Whith the following call I get null though ...
Boris Callens's user avatar
264 votes
12 answers
387k views

Tool to generate JSON schema from JSON data [closed]

We have this json schema draft. I would like to get a sample of my JSON data and generate a skeleton for the JSON schema, that I can rework manually, adding things like description, required, etc, ...
blueFast's user avatar
  • 43.3k

1
2 3 4 5
505