delegate and methods access level

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puzzlecracker

    delegate and methods access level

    Sort of new to internal of delegates.

    My question is when you create a delegate instance, does the
    surrounding block must be able to see/access method you want delegate
    to take on. Here is concrete example:

    namespace Test

    public delegate string MyDelegate (int x);
    class A {

    private string IamPrivate(int x)
    {
    System.WriteLin e("I am private {0}, get lost!",x);
    }
    }


    class B{

    private MyDelegate _d = new MyDelegate (Test.IamPrivat e);

    public void Foo()
    {
    d(10);
    }

    }

    If this is allowed (hopefully not), anyone with ease can circumvent
    the encapsulation of csharp. Is it?

    Well, in nutshell, I am trying to better understand delegate/events in
    the context of a class access level. Explanations are welcome.

    Thanks
  • Peter Duniho

    #2
    Re: delegate and methods access level

    On Sun, 05 Oct 2008 12:59:35 -0700, puzzlecracker <ironsel2000@gm ail.com>
    wrote:
    Sort of new to internal of delegates.
    >
    My question is when you create a delegate instance, does the
    surrounding block must be able to see/access method you want delegate
    to take on.
    Yes, of course it must. Using an identifier as an argument for a
    constructor or any other purpose doesn't change the accessibility rules.
    Why would it?
    Here is concrete example:
    >
    namespace Test
    >
    public delegate string MyDelegate (int x);
    class A {
    >
    private string IamPrivate(int x)
    {
    System.WriteLin e("I am private {0}, get lost!",x);
    }
    }
    >
    >
    class B{
    >
    private MyDelegate _d = new MyDelegate (Test.IamPrivat e);
    >
    public void Foo()
    {
    d(10);
    }
    >
    }
    >
    If this is allowed (hopefully not), anyone with ease can circumvent
    the encapsulation of csharp. Is it?
    Well, did you bother trying to compile it?

    Why ask a question that is easily answered simply by writing the code
    yourself and trying it? Granted, the code you posted has a few other
    problems unrelated to your question (using instance members as if they are
    static, using the namespace name instead of the class name, and using the
    wrong field name when trying to invoke the delegate), but presumably you'd
    have fixed those had you gotten around to actually trying to compile the
    code.

    IMHO, a more interesting question would have been to ask whether the
    following would work:

    delegate string MyDelegate(int x);

    class A
    {
    public static MyDelegate GetDelegate()
    {
    return new MyDelegate(IamP rivate);
    }

    private static string IamPrivate(int x)
    {
    System.WriteLin e("I am private {0}, get lost!",x);
    }
    }

    class B
    {
    private MyDelegate _d = A.GetDelegate() ;

    public void Foo()
    {
    _d(10);
    }
    }

    The answer in this case is, yes...it works. To the beginner, that might
    be non-obvious, since it allows class B to call a private member of class
    A. But in fact, this is one of the reasons delegates are so useful; they
    allow a class to temporarily expose something that might otherwise be
    private, on a case-by-case basis.

    The example you provided seems uninteresting to me. Both because it
    should be obvious that the use of the delegate doesn't change anything at
    all about how class B accesses something in class A, and because it would
    have been trivial to just try it to see. The example I gave could be
    tested just as easily, but the result is potentially contrary to what one
    might expect, and so the outcome is more interesting.

    All IMHO, of course. :)

    Pete

    Comment

    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

      #3
      Re: delegate and methods access level

      puzzlecracker wrote:
      Sort of new to internal of delegates.
      >
      My question is when you create a delegate instance, does the
      surrounding block must be able to see/access method you want delegate
      to take on. Here is concrete example:
      >
      namespace Test
      >
      public delegate string MyDelegate (int x);
      class A {
      >
      private string IamPrivate(int x)
      {
      System.WriteLin e("I am private {0}, get lost!",x);
      }
      }
      >
      >
      class B{
      >
      private MyDelegate _d = new MyDelegate (Test.IamPrivat e);
      >
      public void Foo()
      {
      d(10);
      }
      >
      }
      >
      If this is allowed (hopefully not), anyone with ease can circumvent
      the encapsulation of csharp. Is it?
      >
      Well, in nutshell, I am trying to better understand delegate/events in
      the context of a class access level. Explanations are welcome.
      >
      Thanks
      I suppose that you were trying to use the method in the A class? There
      is no member IamPrivate in the Test namespace...

      The code that creates the delegate instance has to have access to the
      method. The code that uses the delegeate doesn't need to know where the
      method is defined.

      --
      Göran Andersson
      _____
      Göran Anderssons privata hemsida.

      Comment

      • Peter Morris

        #4
        Re: delegate and methods access level

        Does this compile?

        int a = "Hello";

        I hope not!

        My point is you took the time to type it into your newsreader, you could
        just have easily typed it into VS and checked what happened!



        Comment

        Working...