reflection

How to find the first declaring method for a reference method

Suppose you have a generic interface and an implementation: public interface MyInterface<T> { void foo(T param); } public class MyImplementation<T> implements MyInterface<T> { void foo(T param) { } } These two types are framework types I provide. In the next step I want allow users to extend that interface as well as redecla...

Calling function dynamically by using Reflection

Hi, I'm generating dll files contain code like the following example : using System; using System.Collections; using System.Xml; using System.IO; using System.Windows.Forms; namespace CSharpScripter { public class TestClass : CSharpScripter.Command { private int i=1; private int j=2; public int k=3; public TestClass6...

Modify static data members from another process?

In .Net, is it possible for process A to modify the values of some static data members from process B? When running under the same AppDomain, it's possible to do so via Reflection, but about doing so between different process? (via Reflection also. I'm not looking to use functions such as Win32::ReadProcessMemory) ...

VB.NET, templates, reflection, inheritance, feeling adrift

I've just made myself up a problem and am now wondering how to solve it. To begin with, I'm using some third-party components, including some calendar controls like schedule and timeline. They're used in the project classes more or less like that: Friend Class TimeBasedDataView 'some members End Class Friend Class ScheduleDataView...

Is it bad practice to use Reflection in Unit testing?

During the last years I always thought that in Java, Reflection is widely used during Unit testing. Since some of the variables/methods which have to be checked are private, it is somehow necessary to read the values of them. I always thought that the Reflection API is also used for this purpose. Last week i had to test some packages an...

How to tell Reflectively if an Attribute has a public Setter

Im saving an object model out to XML but when i load it back in I get exceptions when trying to use PropertyInfo.SetValue() because the property doesn't have a setter just a getter. I want to either not save out the properties that only have getters or figure out on load whether its valid for me to try and set a value or not. Anybody k...

determine complex type from a primitive type using reflection

I am writing a tool where I need to reflect upon methods and if the parameters of the methods are complex type, then I need to certain type of actions such as instantiating them etc. Now I saw the IsPrimitive property in the Type variable. However, it shows string and decimal as complex types, which technically isn't incorrect. However ...

How to Retrieve Method's Signature in Squeak

printThisMethodSig: aSomething stack := thisContext stackOfSize: 2. Transcript show: (stack at: 2); cr. stack at: 2 returns the method context of the current method. It is possible to retrieve the compiled method of the current method using method message. I want to be able to print the whole signature of the method, for example: from:...

Reflection: get Static method from the parent class.

Hello, I have task to get static method using reflection like this : myType.GetMethod("MyMethod",BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod); In case if class contains MyMethod all works correctly, but in case if parent class contains MyMethod I receive null :(. How can I call static method from the parent u...

How to detect when an attribute is modified by calling reflection methods

I have the following class public class MyClass { private int myAttr; public void setAttr(int a) { myAttr = a; Thread.dumpStack(); } } That way I can detect any attempt to modify myAttr. Well, almost any. It does not work when someone modifies myAttr using Field.set() method. How can I trap Java reflection usag...

Parsing values from XML into types of Type

check this out Type configPropType = configurableProp.getPropertyType(); string attValue = xmlelement.GetAttribute(configurableProp.getName()); configProps[configurableProp.getName()] = attValue; At the point where I am setting the value that got read in from XML it turns out the assigning object needs to be parsed to the correct type...

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 things) a method called Execute for accessing parts of the app not included in its published remote interface. Here is how the app designate...

Change classloader

I'm trying to switch the class loader at runtime: public class Test { public static void main(String[] args) throws Exception { final InjectingClassLoader classLoader = new InjectingClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Thread thread = new Thread("test") { publi...

How to extract object reference from property access lambda

Here's a follow-up question to http://stackoverflow.com/questions/2820660/get-name-of-property-as-a-string. Given a method Foo (error checking omitted for brevity): // Example usage: Foo(() => SomeClass.SomeProperty) // Example usage: Foo(() => someObject.SomeProperty) void Foo(Expression<Func<T>> propertyLambda) { var me = propert...

Reflection PropertyInfo GetValue call errors out for Collection<> type property.

Hey Guys, I have a propertyInfo object and I try to do a GetValue using it. object source = mysourceObject //This object has a property "Prop1" of type Collection<>. var propInfo = source.GetType().GetProperty("Prop1"); var propValue = prop.GetValue(this, null); // do whatever with propValue // ... I get error at the GetValue() ca...

Extension Method for copying properties form object to another, with first attempt

I'm trying to write an extension method that I can use to copy values from one object property to another object of a different type, as long as the property names and types match exactly. This is what I have: public static T CopyFrom<T>(this T toObject, object fromObject) { var fromObjectType = fromObject.GetType(); ...

How to use reflection to call a method and pass parameters whose types are unknown at compile time?

I'd like to call methods of a class dynamically with parameter values that are "parsed" from a string input. For example: I'd like to call the following program with these commands: c:>myprog.exe MethodA System.Int32 777 c:>myprog.exe MethodA System.float 23.17 c:>myprog.exe MethodB System.Int32& 777 c:>myprog.exe MethodC System.Int...

Reflective Generic Detection

Trying to find out if a provided Type is of a given generic type (with any generic types inside) Let me Explain: bool IsOfGenericType(Type baseType, Type sampleType) { /// ... } Such that: IsOfGenericType(typeof(Dictionary<,>), typeof(Dictionary<string, int>)); // True IsOfGenericType(typeof(IDictionary<,>), typeof(Dictionary<st...

How would I use reflection to call all the methods that has a certain custom attribute?

I have a class with a bunch of methods. some of these methods are marked by a custom attribute. I would like to call all these methods at once. How would I go about using reflection to find a list of all the methods in that class that contains this attribute? ...

How can I programatically (using reflection?) change a method body and save my changes back to disk

I can get at the method body easily enough using reflection Type type = assembly.GetType("Lorem.Ipsum.Dolor.Sit"); MethodInfo methodInfo = type.GetMethod("Amet"); MethodBody methodBody = methodInfo.GetMethodBody(); How can I programatically change the method body and save my changes back to disk? ...