Reflection is the process by which a program can observe and modify its own structure and behavior at runtime.
161
votes
12answers
147k views
Java how to: Generic Array creation
Due to the implementation of Java Generics you can't have code like this. How can I implement this while maintaining type safety?
public class GenSet<E> {
private E a[];
public ...
146
votes
4answers
46k views
How to use reflection to call generic Method?
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 ...
39
votes
16answers
33k views
How can I read the properties of a C# class dynamically?
I can do an eval("something()"); to execute the code dynamically in JavaScript. Is there a way for me to do the same thing in C#?
What I am exactly trying to do is that I have an integer variable ...
123
votes
10answers
166k 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 ...
103
votes
8answers
48k views
Can you find all classes in a package using reflection?
A beginner question about reflection, I suppose:
Is it possible to find all classes or interfaces in a given package? (Quickly looking at e.g. Package, it would seem like no.)
66
votes
25answers
40k 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 ...
161
votes
10answers
94k 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!
170
votes
15answers
152k 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 ...
82
votes
5answers
31k 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 at run time.
using reflection i get this error: java.lang.IllegalAccessException: Can not set static final ...
80
votes
13answers
21k views
How can I find the method that called the current method?
When logging in C#, how can I learn the name of the method that called the current method? I know all about System.Reflection.MethodBase.GetCurrentMethod(), but I want to go one step beneath this in ...
105
votes
6answers
82k views
Get property value from string using reflection in C#
I am trying implement the Data transformation using Reflection example in my code.
The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties ...
328
votes
10answers
63k 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 ...
29
votes
6answers
19k views
Getting the size of a field in bytes with C#
I'm having a class which I want to inspect it's fields, and report eventually how much bytes does each field take. I assume all fields are of types as Int32, byte etc.
How can I find out easily how ...
21
votes
9answers
21k views
How can I evaluate a C# expression dynamically?
I would like to do the equivalent of:
object result = Eval("1 + 3");
string now = Eval("System.DateTime.Now().ToString()") as string
Following Biri s link, I got this snippet (modified to remove ...
38
votes
10answers
31k 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 ...