reflection

Detect if a method was overridden using Reflection (C#)

Say I have a base class TestBase where I define a vistual method TestMe() class TestBase { public virtual bool TestMe() { } } Now I inherit this class: class Test1 : TestBase { public override bool TestMe() {} } Now, using Reflection, I need to find if the method TestMe has been overriden in child class - is it possible? ...

Getting class Type information for Elements on a collection

I would like to get gain access to the type of Object held in a Collection. Below is a simplified example of when and why I might want to do this. Is this even possible? List<Address> addressList = new LinkedList<Address>(); Main.addElement(addressList); Class Main{ public void addElement(Object inArgument){ List<Object> ar...

Can you get a Func<T> (or similar) from a MethodInfo object?

UPDATE: The suggestion to use an expression tree to construct a lambda using the given MethodInfo, in conjunction with the Expression<TDelegate>.Compile method, proved to be a gold mine in my scenario. In the specific case where I was toying with this idea, I saw average execution time for a particular method go from ~0.25 ms to ~0.001 m...

Could not load file or assembly

Hi guys, I'm tring to create a generic collection of dynamic type at runtime of Silverlight application. My code: Type listType = Type.GetType("System.Collections.ObjectModel.ObservableCollection`1[[" + type.AssemblyQualifiedName + "]], System.Windows, Version=2.0.5.0, Culture=neutral, PublicK...

Getting an argument list for a class method

What I'd like is for this class class Car { public function __construct(Engine $engine, Make $make, Model $model) { // stuff } } Get an array that has the types needed to construct this car (Engine, Make, Model) in the order they are needed for the constructor. I'm using this for a Dependency Injection-esque thin...

Is this an F# quotations bug?

[<ReflectedDefinition>] let rec x = (fun() -> x + "abc") () The sample code with the recursive value above produces the following F# compiler error: error FS0432: [<ReflectedDefinition>] terms cannot contain uses of the prefix splice operator '%' I can't see any slicing operator usage in the code above, looks like a bug... :) Lo...

Set fields with instrospection - Problem with String.valueOf(String)

Hey there! I'm setting public fields of the Object 'this' via reflection. Both the field name and the value are given as String. I use several various field types: Boolean, Integer, Float, Double, an own enum, and a String. It works with all of them except with a String. The exception that gets thrown is that no method with the Signatur...

help with reflections and annotations in java

I'm having trouble with doubling up on my code for no reason other than my own lack of ability to do it more efficiently... for (Method curr: all){ if (curr.isAnnotationPresent(anno)){ if (anno == Pre.class){ for (String str : curr.getAnnotation(Pre.class).value()){ if (str.equals(...

How do I iterate over an Array field reflectively?

I have Class<? extends Object> class1 = obj.getClass(); Field[] fields = class1.getDeclaredFields(); for (Field aField : fields) { aField.setAccessible(true); if (aField.getType().isArray()) { for (?? vals : aField) { System.out.println(vals); } } } ...

.NET sources to download

Where can I download source code for the .NET Framework? I mean sources of libraries. I need the source for reflection methods like invoke and other. ...

Json Convert To and From Query String with jquery?

I have a string like a=6&id=99 (i might store it in html as 'a=6&id=99' however thats not what js will see). I would like to convert that string into an object so i can do func(o.a); or o.id=44; How do i do that? Part 2: How do i convert that object back to a query string? it would probably be trivial code that i can write. ...

Call a function by its name, given from string java

I would like to be able to call a function based on its name provided by a string. Something like public void callByName(String funcName){ this.(funcName)(); } I have searched a bit into lambda funcions but they are not supported. I was thinking about going for Reflection, but I am a bit new to programming, so I am not so familiar ...

Java - Class type from inside static initialization block

Is it possible to get the class type from inside the static initialization block? This is a simplified version of what I currently have:: class Person extends SuperClass { String firstName; static{ // This function is on the "SuperClass": // I'd for this function to be able to get "Person.class" without me //...

Check if the superclass is java.lang.Object

I use if (clazz.getSuperclass().getName() == "java.lang.Object") Is there a better way? ...

How do I print the method body reflectively?

Right now I have private static void getMethods(Class<? extends Object> clazz) { Method[] declaredMethods = clazz.getDeclaredMethods(); for (Method aMethod : declaredMethods) { aMethod.setAccessible(true); // Print the declaration System.out.print(Modifier.toString(aMethod.getModifiers()) + " " + aMe...

How do I instantiate Class Dynamically in Java?

I have the class name stored in a property file. I know that the classes store will implement IDynamicLoad. How do I instantiate the class dynamically? Right now I have Properties foo = new Properties(); foo.load(new FileInputStream(new File("ClassName.properties"))); String class_name = foo.getProperty("class","DefaultCla...

Adding values to a Dictionary using reflection.

I have a class class a { private Dictionary <int , string> m_Dict = new Dictionary<int , string>(); } from some other component/class need to add values to the m_Dict dictionary using reflection! How do i do it ? i searched and tried but was not successfull. ...

Reflection for a Field going wrong

Hi, I have been trying to use reflection for a specifiec Field in the android.os.build class, the MANUFACTURER field... I have tried by using this code : try { Class myBuildClass = android.os.Build.class; Field m1 = Build.class.getDeclaredField("MANUFACTURER"); validField = true; manufacturer = Build...

How to discover getters and setters on hibernate objects

I need to find a way of taking a hibernate object and discovering at runtime all of the getter methods that relate to persistable fields. I'm using annotations in the classes but have previously had difficulties working with them (I ran into a 2 year old bug the java developers still haven't fixed). Does anyone know how I can do this pl...

Auto-generate WebControls

I want to generate .net code from a template so that it more rapid, so lazy developers (and I mean that in the nicest possible way!) don't have to write them in the IDE, compile them, etc... I know I can roll my own tool which generates the code using reflection (by reading in some text file, etc), but I just wondered if there was an ea...