So the way to work with DLR is to call the Type context so that it can determine which methods are available. By default, impromptu uses the type of the object you are calling, so it usually works with most private methods, but obviously not from the base classes.
In your case, you need to create your own improvisation context, which is mentioned in the UsagePrivate documentation, it works later as the interfaces. It is also unclear from the documentation, but the fact is that you can pass a typeof () object for context. So in your example you can:
var context = InvokeContext.CreateContext; Console.WriteLine(Impromptu.InvokeMember(context(type, typeof(OtherType)), "Method", 2));
If you need to do this for general cases, itโs not very, but you can always catch the exception and recursively try the base type, since in the general case it works for the first time, there should be no slowdown, and class hierarchies are usually not very deep, and since you just do it interactively once, not thousands of times, it should be fine.
var context = InvokeContext.CreateContext; var type = target.GetType() while(true){ try{ Console.WriteLine(Impromptu.InvokeMember(context(target, type), "Method", 2)); break; }catch(RuntimeBinderException ex){ type = type.BaseType; if(type ==null) throw ex; } }
source share