C# Dynamic Event Subscription

You can compile expression trees to use void methods without any arguments as event handlers for events of any type. To accommodate other event handler types, you have to map the event handler’s parameters to the events somehow. using System; using System.Linq; using System.Linq.Expressions; using System.Reflection; class ExampleEventArgs : EventArgs { public int IntArg {get; … Read more

How to use Reflection to Invoke an Overloaded Method in .NET

You have to specify which method you want: class SomeType { void Foo(int size, string bar) { } void Foo() { } } SomeType obj = new SomeType(); // call with int and string arguments obj.GetType() .GetMethod(“Foo”, new Type[] { typeof(int), typeof(string) }) .Invoke(obj, new object[] { 42, “Hello” }); // call without arguments obj.GetType() … Read more

Best way of invoking getter by reflection

I think this should point you towards the right direction: import java.beans.* for (PropertyDescriptor pd : Introspector.getBeanInfo(Foo.class).getPropertyDescriptors()) { if (pd.getReadMethod() != null && !”class”.equals(pd.getName())) System.out.println(pd.getReadMethod().invoke(foo)); } Note that you could create BeanInfo or PropertyDescriptor instances yourself, i.e. without using Introspector. However, Introspector does some caching internally which is normally a Good Thing ™. If you’re … Read more

Using reflection to change static final File.separatorChar for unit testing?

From the documentation for Field.set: If the underlying field is final, the method throws an IllegalAccessException unless setAccessible(true) has succeeded for this field and this field is non-static. So at first it seems that you are out of luck, since File.separatorChar is static. Surprisingly, there is a way to get around this: simply make the … Read more

TypeOf without an instance and passing result to a func

Yes, it’s possible. The trick is to start from a pointer to the type (whose value can be a typed nil, that’s perfectly OK), and then use Type.Elem() to get the reflect.Type descriptor of the pointed type (the base type). See some examples: t := reflect.TypeOf((*int)(nil)).Elem() fmt.Println(t) t = reflect.TypeOf((*http.Request)(nil)).Elem() fmt.Println(t) t = reflect.TypeOf((*os.File)(nil)).Elem() fmt.Println(t) … Read more

How to get defined operators for a type in .net

Get the methods with Type.GetMethods, then use MethodInfo.IsSpecialName to discover operators, conversions etc. Here’s an example: using System; using System.Reflection; public class Foo { public static Foo operator +(Foo x, Foo y) { return new Foo(); } public static implicit operator string(Foo x) { return “”; } } public class Example { public static void … Read more

How to watch a variable for changes

You cannot. There is no watch-on-modification hook built into Java itself. Obviously, you could do polling, though. But then it won’t be “live”. AspectJ may allow such a think, but I’m not sure whether it holds for primitive variables, or only when you are using getters and setters. The clean Java-way is to make the … Read more