Skip to content
jbtule edited this page May 25, 2013 · 1 revision

By default, when using Dynamic.InvokeMember, if the member name matches up with private, protected or internal methods, they will be callable. This allows you to call things like Anonymous Types which are internal and keeps the API simple while ensuring that internal/protected access members are never blocked when you should conceptually have access.

This is not done by any evil hacks, it's done by using the DLR, because when you make a dynamic invocation the DLR requires a context type as a parameter to determine permission and Dynamic.InvokeMember always uses the type of the target object. Since this uses the DLR it actually allows you to call private members in Silverlight something that reflection won't let you do. If for any reason you want to preserve permissions you can wrap your object with anInvokeContext that lets you specify the context.

Example


public class TestWithPrivateMethod
    {
        private int Test()
        {
            return 3;
        }
    }


    public interface IExposePrivateMethod
    {
        int Test();
    }

...

     var tTest = new TestWithPrivateMethod();

     var tExposedReallyLate = Dynamic.InvokeMember(tTest,"Test"); //Also Works
     var context = InvokeContext.CreateContext;//InvokeContext delegate
     var tTest = new TestWithPrivateMethod();

     var tExposedReallyLate = Dynamic.InvokeMember(context(tTest, this),"Test");  //throws Runtime Binding Exception