none
method access security .NET 4 RRS feed

  • Question

  • Does .NET 4 have built in capability for method access security as in this example, I have 3 objects and one static method

    ObjectA.MethodA();

    ClassA.StaticMethodA()

    ObjectB.MethodA();

    ObjectB.MethodB();

    ObjectC.MethodC();

     

    Can I do this: 

    1) deny calls to  ObjectC.MethodC() to all but ObjectA.MethodA()

    2) deny calls to ClassA.StaticMethodA() to all but ObjectB.MethodA(); (deny to ObjectB.MethodB() too)

    I do not need to  implement permission  on low level actions in OS like write to disk or something like that, I only need to implement

    method access permissions between my own methods. TIA



    Monday, June 27, 2011 4:22 PM

Answers

  • I'm not aware of any functionality that do what you want. However you can use a trick to solve the problem, inside ObjectC.MethodC use the StackTrace object to know what is the method that is calling your code, and deny the execution if is not the method ObjectA.MethodA. Same thing for static methods.

    The following snipped of code do what just described:
    static void Foo()
    {
    Bar();
    }

    static void Bar()
    {
    var frame = new StackFrame( 1, false );
        var method = frame.GetMethod();
        var name = method.Name;
        Console.WriteLine( name ); // print Foo
    }

     

    • Proposed as answer by Antonio Parata Wednesday, June 29, 2011 9:51 AM
    • Marked as answer by Paul Zhou Tuesday, July 5, 2011 5:37 AM
    Monday, June 27, 2011 8:02 PM