Allowing a particular class in other namespace to access a method non public method of a class in other namespace

Could in any way I can enable a particular class in other namespace to access a method(non public) of another class in other namspace
That is suppose I have a namespace1 with class1
and a namespace2 with class2
class1 contain a method named OnlyAccessibleByClass2
Could I do it


Answer this question

Allowing a particular class in other namespace to access a method non public method of a class in other namespace

  • bdaniel7

    I have a class project mytestclasses
    In it I have two namespaces
    namespace1
    namespace2

    In namespace1 Ii have three classes
    class1nmsps1
    class2nmsps1 and
    class3nmsps1

    and In namespace2 I have three classes
    class1nmsps2
    class2nmsps2 and
    class3nmsps2

    In class class1nmsps1 I have a function named OnlyVisibleToMyChildsAndClass3nmsps2

    From my method name I think I have able to clear my question now.


  • vuloc_nguyen

    I Think John you are right.

    Actually I want's to convey samething that it isn't possible.We have to declare our class public.

    But Now My Coleague is saying it will be achievable if we declare our class

    protected internal.

    Is it correct or not

     



  • Newsha

    If the purpose is that you have two assemblies A and B, which somebody is supposed to use and A uses (parts) of B which should not be visible to the "enduser" of A and B, take a look at the Attribute InternalsVisibleToAttribute.

    It enables you to make internal classes of B visible to A, but not to any other user of B.


  • Navi73660

    I will create the instances of both class.
    What I simply wants is to let a particular function to be access by a particular class in other namespace.
    Other things I have explain in the question


  • Trond Elde

    Create a static class somewhere, and use that to let class2 store the information that something needs to be done.

    Now let class1 regularly check the static class, and call the method from class1 when needed.


  • Sitanshu

    if you want to access the Function without creating the Class Object, then you have to make that function as static and can access it like

    namespace2.class2.OnlyAccessibleByClass2();



  • Sandhya Venkat

    You cannot declare the class itself as private, protected or proteced internal. If a class has no protection modifier at all it is implicitly private to the generated library, but not visible at all to any users of the library. Putting protected internal on the function you want to be available to the other class is also not OK as the function would then be only visible to the class itself and to classes inheriting from it.

    Just make the function itself "internal" like I said before, that's the closest you can get, e.g.:

    class Class1
    {
    public void Test1() { ... }
    internal void Test2() { ... }
    }

    class Class2
    {
    public void Test3() { ...}
    }

    If both classes are in one library, you achieve that Class2 can call Class1.Test2 but Class1.Test2 is still private/internal to the library.


  • Colin McC

    Making something visible to a special class only is not possible. If anything is like you said in one library, the closed I think, is to make the method "internal" that would make it invisible to anything outside the library, but any class inside the library could use it, not only class3ns2.
  • Saravanan.W.S

    You cannot directly access it, bcoz it is not a static class nor a static function

    So, in namespace1.class1 , you have to create a object of namespace2.class2
    then you can access the OnlyAccessibleByClass2 Function.



  • Terry Lewis

    You cannot designate a specific namespace or class that can access a particular method. However, if you specify it as internal, only classes within the same assembly or any friend assemblies can access that method.
  • Philippe Cand

    try the following code hope it will clarify ur doubts

    using System;
    namespace namespace1
    {
    class class1
    {
    public void method1(){Console.WriteLine("Hello N1.Class1.Method1"); }
    }
    class class2
    {
    public void method2() { Console.WriteLine("Hello N1.Class2.Method2.");}
    }
    class class3
    {
    public void method3() {Console.WriteLine("Hello N1.Class3.Method3."); }
    }
    }

    namespace namespace2
    {
    class class1
    {
    public void method1(){Console.WriteLine("Hello N2.Class1.Method1"); }
    }
    class class2
    {
    public void method2() {
    namespace1.class1 c1 = new namespace1.class1();
    c1.method1();
    Console.WriteLine("Hello N2.Class2.Method2.");}
    }
    class class3
    {
    public void method3() {Console.WriteLine("Hello N2.Class3.Method3."); }
    }
    }
    namespace mainN
    {
    class mainClass
    {
    static void Main()
    {
    namespace2.class2 c2 = new namespace2.class2();
    c2.method2();
    }
    }
    }




  • Allowing a particular class in other namespace to access a method non public method of a class in other namespace