6

Is there any way in C# to specify a method that can be accessed only from derived classes of the same assembly without using internal access modifier?

Thanks.

abenci
  • 8,422
  • 19
  • 69
  • 134

6 Answers6

3

You have to specify both internal as well as protected.

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
  • Check this please: http://kossovsky.net/index.php/2009/06/cshar-protected-internal/ – abenci May 26 '11 at 07:47
  • 1
    Sorry, I just checked, By typing it Internal Protected. It can be accessed directly from Base Class. public class Bar { internal protected void MyFun() { } } Bar f = new Bar(); f.MyFun(); – Pankaj May 26 '11 at 07:49
  • @Akash Kava: Sorry, that wont solve the problem. Here, access is limited to the current assembly or types derived from the containing class anywhere !! – Akram Shahda May 26 '11 at 08:09
  • Yes but there is no other way, this is the max you can do. – Akash Kava May 26 '11 at 08:55
  • @devdept, I know what you are saying, but internal means you are the owner and there is no point in restricting yourself in your own project, there is no other way, what are you trying to prove here? – Akash Kava May 30 '11 at 07:46
1

Give the scope of the class as internal and method scope as protected

Internal class Myclass
{
    Protected void MyMethod()
    {
        //Do something
    }
}
Umesh CHILAKA
  • 1,466
  • 14
  • 25
0

Read about C# Protected Internal

protected A protected member is accessible within its class and by derived classes.

internal Internal types or members are accessible only within files in the same assembly.

protected internal Access is limited to the current assembly or types derived from the containing class. * protected internal is the only access modifiers combination allowed for a member or a type.

So you need to use internal key word after all:

protected internal memberName(){ ... };
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
0

Ok! I imagine the situation where you would like to have more flexible control of what assembly class is from, and what class is derived from, than with 'internal' keyword. For example if you wish to check that the object is from th-a-at assembly and derives from one of the-e-e-ese classes.

You can do it through Assembly.GetAssembly() method of the type
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getassembly.aspx

And here is the way to check if object is derived one from another.
Check if a class is derived from a generic class

So you can check all you need.

Then you can implement something like:

public static bool CheckTypeIsCorrect(Type t)
{
    if(t.GetAssembly().FullName != AssemblyYouNeed) return false;
    if(!(check anything you want in the class)) return false;
    return true;
}
...
public void YourMethod(object value)
{
    if(!CheckTypeIsCorrect(typeof(value))) 
        throw ArgumentException("Type of the object is not correct");
    ...
}
Community
  • 1
  • 1
MajesticRa
  • 13,770
  • 12
  • 63
  • 77
0

Make the function Access modifier as Protected in your Base Class. Like below

protected void YourFunctionName()
{
//This will be accessible in your derived class only.
}

A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

A protected member of a base class is accessible in a derived class only if the access takes place through the derived class type.

Below is an example from MSDN

namespace SameAssembly
{
    public class A 
    {
       protected void abc()
       {
       }
    }

    private class B : A 
    {
       void F() 
       {
          A a = new A();  
          B b = new B();  
          a.abc();   // Error
          b.abc();   // OK
       }
    }
}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
0

I believe you can do something like:

public class OriginalClass { ... }

internal class DerivedClass: OriginalClass
{
    protected void MemberName() { ... }
}

This way, only internal classes can see the DerivedClass and only internal derived classes can see the portected members inside it.

And also, you still able to access the OriginalClass from everywhere.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65