none
Deny reflection permission to Invoke target RRS feed

  • Question

  • Is it possible to explicitly deny ReflectionPermission to an invokee when Invoke call will requires it (E.g target method is private)? This obviously does not work:

    public class OtherClass
    {
    private static void PrivateMethod()
    {
    System.Console.WriteLine("PrivateMethod reached");
    }
    }



    public class Program
    {
    public static void Main(string[] args)
    {
    System.Security.PermissionSet ps =
    new System.Security.PermissionSet(
    System.Security.Permissions.PermissionState.None);
    ps.AddPermission(
    new System.Security.Permissions.ReflectionPermission(
    System.Security.Permissions.ReflectionPermissionFlag.AllFlags));


    ps.Deny();

    typeof(OtherClass).
    GetMethod("PrivateMethod",
    System.Reflection.BindingFlags.NonPublic |
    System.Reflection.BindingFlags.Static).
    Invoke(null, null);
    }
    }
    b/c Invoke requires ReflectionPermission. So a MethodAccessException is thrown. Is there a way to deny reflection permission to the callee without preventing the invocation in the caller?
    Tuesday, November 16, 2010 12:35 AM

All replies