Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

UsagePrivate

jbtule edited this page · 1 revision

By default, when using ActLike<Interface>(), if the interface matches up with private, protected or internal methods, they will be callable. This allows you to put an interface on things like Anonymous Types which are internal and keeps the API simple and still makes sure that internal/protected access members are never blocked when you should conceptually have access but due to the proxy you actually don't.

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 ActLike 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.

Also [UsageReallyLateBinding Really Late Binding] can also call private/protected/internal properties.

Example


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


    public interface IExposePrivateMethod
    {
        int Test();
    }

...


     var tTest = new TestWithPrivateMethod();
     var tExposed = tTest.ActLike<IExposePrivateMethod>();
     tExposed.Test() //Works

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

     var tNonExposed = context(tTest, this).ActLike<IExposePrivateMethod>();
     tExposed.Test() //throws Runtime Binding Exception

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

Something went wrong with that request. Please try again.